简体   繁体   English

Windows Phone解析json数据? 从用户输入的网址?

[英]Windows Phone Parse json data? from Url with user input?

I Followed this to Create Restful Web Services which display JSON as OutPut from the MySQL Data base. 我遵循此步骤创建了Restful Web Services,该服务将MySQL数据库中的JSON显示为OutPut。

I Successfully Done it, But Here I Have nearly 100 Tables in Database with different name 我成功完成了,但是在数据库中我有将近100个不同名称的表

And I am Getting This kind of Json Data 我正在获取这种Json数据

{"WindowsResult":[{"ID":9,"TYPE":"WindowsPhone","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post1.jpg"}],"success":3}

this another result 这另一个结果

{"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3}

this another one 这另一个

{"SamsungResult":[{"ID":1,"TYPE":"Samsung","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post3.jpg"}],"success":3}

For this I am parsing a JSON data form a url like localhost/Service1.svc/windows or localhost/Service1.svc/sony like that 为此,我正在解析JSON数据形式的url,例如localhost / Service1.svc / windows或localhost / Service1.svc / sony这样的URL

Here it should Take with a user input.. 在这里它应该带有用户输入。

private void btnAdd_Click(object sender, RoutedEventArgs e)
{

string Data = txtData.Text;
string ServiceUri = "http://lhost:30576/Service1.svc/"
+ "Data" + "/";
WebClient proxy = new WebClient();
proxy.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(proxy_DownloadStringCompleted);
proxy.DownloadStringAsync(new Uri(ServiceUri));

}

But I am Confused at Here 但是我在这里很困惑

void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
 {
 var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result); 
 foreach (var TYPE in rootObject.SonyResult) 
 { 
 Console.WriteLine(type.TITLE);
 } 
 }

what should be at Root object 根对象应该是什么
for some Results I have SamsungResult as Root Object like that. 对于某些结果,我将SamsungResult作为根对象。
Please suggest Me Regarding this.. 请就此向我提出建议。

Update 更新资料

when I enter the a url like http://www.google.com/service1.scv/sony 当我输入类似http://www.google.com/service1.scv/sony的网址时

I am getting 我正进入(状态

{"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3}

like that every brand I have one result Not All in one string.. 像这样,每个品牌我都有一个结果不是全部都在一个字符串中。

K bro the problem is in your json data K Bro问题出在您的json数据中

{"Mobiles":[
   {"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3},
   {"WindowsResult":[{"ID":9,"TYPE":"WindowsPhone","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post1.jpg"}],"success":3}
]}


Since you hadn't specified a Root object name for our tables so json was using your table names as root objects. 由于您尚未为表指定Root对象名称,因此json会将您的表名称用作根对象。
Its C# class will be 它的C#类将是

public class SonyResult
{
public int ID { get; set; }
public string TYPE { get; set; }
public string TITLE { get; set; }
public string PRICE { get; set; }
public string IMAGE { get; set; }
}
public class WindowsResult
{
public int ID { get; set; }
public string TYPE { get; set; }
public string TITLE { get; set; }
public string PRICE { get; set; }
public string IMAGE { get; set; }
}
public class Mobile
{
public List<SonyResult> SonyResult { get; set; }
public int success { get; set; }
public List<WindowsResult> WindowsResult { get; set; }
}
public class RootObject
{
public List<Mobile> Mobiles { get; set; }
}


Hope that solved your problem. 希望能解决您的问题。

Update 更新资料
For ur received query the C# class is going to be 对于您收到的查询,C#类将是

public class SonyResult 
{public int ID { get; set; }
 public string TYPE { get; set; }
 public string TITLE { get; set; }
 public string PRICE { get; set; }
 public string IMAGE { get; set; }
}
public class RootObject
{
 public List<SonyResult> SonyResult { get; set; }
 public int success { get; set; }
}

Now since for each different models you are getting different queries where each are an array of its own with no common object name. 现在,由于对于每个不同的模型,您将获得不同的查询,其中每个查询都是其自己的数组,没有通用对象名。 So for each different model the root object class will differ. 因此,对于每个不同的模型,根对象类将有所不同。 I havent tried it as I dont have access to your code but try if this format works(Since I havent tried this so I have my doubts if this will work or not). 我没有尝试过它,因为我无法访问您的代码,但是请尝试这种格式是否有效(因为我没有尝试过这种格式,所以我怀疑这是否可行)。

public class SonyResult
{
public int ID { get; set; }
public string TYPE { get; set; }
public string TITLE { get; set; }
public string PRICE { get; set; }
public string IMAGE { get; set; }
}
public class WindowsResult
{
public int ID { get; set; }
public string TYPE { get; set; }
public string TITLE { get; set; }
public string PRICE { get; set; }
public string IMAGE { get; set; }
}
public class RootObject
{
public List<SonyResult> SonyResult { get; set; }
public int success { get; set; }
public List<WindowsResult> WindowsResult { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM