[英]How to Parse Json data with Images in Windows Phone 8 from a URL? Dynamic data
[英]Windows Phone Parse json data? from Url with user input?
我遵循此步驟創建了Restful Web Services,該服務將MySQL數據庫中的JSON顯示為OutPut。
我成功完成了,但是在數據庫中我有將近100個不同名稱的表
我正在獲取這種Json數據
{"WindowsResult":[{"ID":9,"TYPE":"WindowsPhone","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post1.jpg"}],"success":3}
這另一個結果
{"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3}
這另一個
{"SamsungResult":[{"ID":1,"TYPE":"Samsung","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post3.jpg"}],"success":3}
為此,我正在解析JSON數據形式的url,例如localhost / Service1.svc / windows或localhost / Service1.svc / sony這樣的URL
在這里它應該帶有用戶輸入。
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));
}
但是我在這里很困惑
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var TYPE in rootObject.SonyResult)
{
Console.WriteLine(type.TITLE);
}
}
根對象應該是什么
對於某些結果,我將SamsungResult作為根對象。
請就此向我提出建議。
更新資料
當我輸入類似http://www.google.com/service1.scv/sony的網址時
我正進入(狀態
{"SonyResult":[{"ID":3,"TYPE":"SonyXperia","TITLE":"XYZ","PRICE":"$0","IMAGE":"Post2.jpg"}],"success":3}
像這樣,每個品牌我都有一個結果不是全部都在一個字符串中。
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}
]}
由於您尚未為表指定Root對象名稱,因此json會將您的表名稱用作根對象。
它的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; }
}
希望能解決您的問題。
更新資料
對於您收到的查詢,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; }
}
現在,由於對於每個不同的模型,您將獲得不同的查詢,其中每個查詢都是其自己的數組,沒有通用對象名。 因此,對於每個不同的模型,根對象類將有所不同。 我沒有嘗試過它,因為我無法訪問您的代碼,但是請嘗試這種格式是否有效(因為我沒有嘗試過這種格式,所以我懷疑這是否可行)。
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.