如何使用.NET解析嵌套的JSON字符串 [英] How to parse nested JSON string using .NET
问题描述
我试图解析使用VB.NET的GCM(谷歌可能会消息)服务器返回一个嵌套的JSON字符串。 JSON字符串看起来是这样的:
I'm trying to parse a nested JSON string returned from the GCM (Google Could Messaging) server using VB.NET. The JSON string looks like this:
{ "multicast_id": 216,
"success": 3,
"failure": 3,
"canonical_ids": 1,
"results": [
{ "message_id": "1:0408" },
{ "error": "Unavailable" },
{ "error": "InvalidRegistration" },
{ "message_id": "1:1516" },
{ "message_id": "1:2342", "registration_id": "32" },
{ "error": "NotRegistered"}
]
}
我想获得在结果数组中的上述字符串。
I would like to get the results array in the above string.
我发现下面的例子有帮助的,<一个href="http://www.offsight.co.uk/tutorials/web-development/asp.net/parse-json-from-a-string-or-post-in-asp.net-vb/"相对=nofollow>例如,但它并没有说明如何去嵌套的部分,特别是的Message_ID 错误和 registration_id 在结果阵列。
I found the following example helpful, example but it does not show how to get to the nested parts, specifically message_id, error and registration_id inside the results array.
感谢
推荐答案
我会使用C#给出一个答案和的 Json.net
I'll give an answer using c# and Json.net
var jobj = JsonConvert.DeserializeObject<Response>(json);
您也可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx"相对=nofollow> JavaScriptSerializer
var jobj2 = new JavaScriptSerializer().Deserialize<Response>(json);
public class Result
{
public string message_id { get; set; }
public string error { get; set; }
public string registration_id { get; set; }
}
public class Response
{
public int multicast_id { get; set; }
public int success { get; set; }
public int failure { get; set; }
public int canonical_ids { get; set; }
public List<Result> results { get; set; }
}
这篇关于如何使用.NET解析嵌套的JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!