简体   繁体   中英

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.

I Successfully Done it, But Here I Have nearly 100 Tables in Database with different name

And I am Getting This kind of Json Data

{"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

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.
Please suggest Me Regarding this..

Update

when I enter the a url like 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

{"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.
Its C# class will be

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

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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