如何使用.NET解析嵌套的JSON字符串 [英] How to parse nested JSON string using .NET

查看:256
本文介绍了如何使用.NET解析嵌套的JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆