我想知道,我们如何解析从其他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