【问题标题】:How to parse nested JSON string using .NET如何使用 .NET 解析嵌套的 JSON 字符串
【发布时间】:2013-05-13 19:53:12
【问题描述】:

我正在尝试使用 VB.NET 解析从 GCM(Google 可以消息传递)服务器返回的嵌套 JSON 字符串。 JSON 字符串如下所示:

{ "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"}
  ]
}

我想在上面的字符串中得到 results 数组。

我发现以下示例很有帮助,example,但它没有显示如何访问嵌套部分,特别是 message_iderrorregistration_id 在 results 数组中。

谢谢

【问题讨论】:

    标签: asp.net .net vb.net json google-cloud-messaging


    【解决方案1】:

    我会用c#和Json.net给出答案

    var jobj = JsonConvert.DeserializeObject<Response>(json);
    

    你也可以使用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; }
    }
    

    【讨论】:

    • @KashifB 使用 Telerik's Code Converter 将 C# 转换为 VB.NET
    • 多播 ID 和其他 ID 应该很长 - 对于谷歌发回的内容来说,int 太小
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    • 2022-01-08
    • 1970-01-01
    相关资源
    最近更新 更多