我想知道,我們如何解析從其他Web服務接收到的JSON數據。示例JSON數據在這裏給出{"success":true, userName:true}
解析Windows Phone 8中的JSON數據
我看到很多關於這方面的文章,因爲我對此很陌生,不知道最好的方法。
在此先感謝 塞巴斯蒂安
我想知道,我們如何解析從其他Web服務接收到的JSON數據。示例JSON數據在這裏給出{"success":true, userName:true}
解析Windows Phone 8中的JSON數據
我看到很多關於這方面的文章,因爲我對此很陌生,不知道最好的方法。
在此先感謝 塞巴斯蒂安
我建議你建立你的JSON類,否則你可以去this網站,並生成類。
一個示例代碼段會是這樣的:
public class RootObject
{
public Response response { get; set; }
}
public class Response
{
public int errorFlag { get; set; }
[JsonProperty("Score Detail")]
public JObject ScoreDetail { get; set; }
}
看一看這篇文章脫穎而出更多: http://blogs.msdn.com/b/africaapps/archive/2013/02/25/parsing-json-in-windows-phone-apps.aspx
Json.NET讓這樣的事情超級簡單 - https://www.nuget.org/packages/newtonsoft.json/
很多易於遵循的例子在這裏 - http://james.newtonking.com/json/help/index.html
如果您使用可視化al studio 2013,您可以使用「粘貼爲json」功能直接生成反序列化的類。 然後,您可以使用此代碼解析JSON
public T GetObject(string json)
{
DataContractJsonSerializer jsonParser = new DataContractJsonSerializer(typeof(T));
byte[] byteArray = Encoding.UTF8.GetBytes(json);
MemoryStream stream = new MemoryStream(byteArray);
var obj = jsonParser.ReadObject(stream);
return (T)obj;
}
http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c – DevBob