【问题标题】:Deserialize json multiple array反序列化json多个数组
【发布时间】:2015-11-10 15:39:20
【问题描述】:

这几天我一直在碰壁。

有人能告诉我如何反序列化吗?我可以反序列化单个 json,但反序列化一个数组让我很难过。

 [
  {
    "msys": {
      "message_event": {
        "type": "bounce",
        "bounce_class": "1",
        "campaign_id": "Example Campaign Name",
        "customer_id": "1",
        "delv_method": "esmtp",
        "device_token": "45c19189783f867973f6e6a5cca60061ffe4fa77c547150563a1192fa9847f8a",
        "error_code": "554",
        "ip_address": "127.0.0.1",
        "message_id": "0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e",
        "msg_from": "sender@example.com",
        "msg_size": "1337",
        "num_retries": "2",
        "rcpt_meta": {
          "customKey": "customValue"
        },
        "rcpt_tags": [
          "male",
          "US"
        ],
        "rcpt_to": "recipient@example.com",
        "rcpt_type": "cc",
        "raw_reason": "MAIL REFUSED - IP (17.99.99.99) is in black list",
        "reason": "MAIL REFUSED - IP (a.b.c.d) is in black list",
        "routing_domain": "example.com",
        "subject": "Summer deals are here!",
        "template_id": "templ-1234",
        "template_version": "1",
        "timestamp": 1427736822,
        "transmission_id": "65832150921904138"
      }
    }
  }
]

我的类定义为:

public class Rootobject
{
    public Class1[] Property1 { get; set; }
}

public class Class1
{
    public Msys msys { get; set; }
}

public class Msys
{
    public Message_Event message_event { get; set; }
}

public class Message_Event
{
    public string type { get; set; }
    public string bounce_class { get; set; }
    public string campaign_id { get; set; }
    public string customer_id { get; set; }
    public string delv_method { get; set; }
    public string device_token { get; set; }
    public string error_code { get; set; }
    public string ip_address { get; set; }
    public string message_id { get; set; }
    public string msg_from { get; set; }
    public string msg_size { get; set; }
    public string num_retries { get; set; }
    public Rcpt_Meta rcpt_meta { get; set; }
    public string[] rcpt_tags { get; set; }
    public string rcpt_to { get; set; }
    public string rcpt_type { get; set; }
    public string raw_reason { get; set; }
    public string reason { get; set; }
    public string routing_domain { get; set; }
    public string subject { get; set; }
    public string template_id { get; set; }
    public string template_version { get; set; }
    public int timestamp { get; set; }
    public string transmission_id { get; set; }
}

public class Rcpt_Meta
{
    public string customKey { get; set; }
}

【问题讨论】:

  • 我现在开始工作了。非常感谢大家的帮助。

标签: c# json json.net json-deserialization


【解决方案1】:

就我而言,这是可行的。您需要告诉 Newtonsoft.Json 您的目标类型是什么(数组 = 列表):

var des = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Class1>>(json);

这是一个简单的完整示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NewtonSoftSample
{
    class Program
    {
        static void Main(string[] args)
        {
            string json =
@"
[
  {
    ""msys"": {
      ""message_event"": {
        ""type"": ""bounce"",
        ""bounce_class"": ""1"",
        ""campaign_id"": ""Example Campaign Name"",
        ""customer_id"": ""1"",
        ""delv_method"": ""esmtp"",
        ""device_token"": ""45c19189783f867973f6e6a5cca60061ffe4fa77c547150563a1192fa9847f8a"",
        ""error_code"": ""554"",
        ""ip_address"": ""127.0.0.1"",
        ""message_id"": ""0e0d94b7-9085-4e3c-ab30-e3f2cd9c273e"",
        ""msg_from"": ""sender@example.com"",
        ""msg_size"": ""1337"",
        ""num_retries"": ""2"",
        ""rcpt_meta"": {
          ""customKey"": ""customValue""
        },
        ""rcpt_tags"": [
          ""male"",
          ""US""
        ],
        ""rcpt_to"": ""recipient@example.com"",
        ""rcpt_type"": ""cc"",
        ""raw_reason"": ""MAIL REFUSED - IP (17.99.99.99) is in black list"",
        ""reason"": ""MAIL REFUSED - IP (a.b.c.d) is in black list"",
        ""routing_domain"": ""example.com"",
        ""subject"": ""Summer deals are here!"",
        ""template_id"": ""templ-1234"",
        ""template_version"": ""1"",
        ""timestamp"": 1427736822,
        ""transmission_id"": ""65832150921904138""
      }
    }
  }
]
";

            var des = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Class1>>(json);

            Console.WriteLine("Done");

            Console.ReadLine();

        }
    }

    public class Rootobject
    {
        public Class1[] Property1 { get; set; }
    }

    public class Class1
    {
        public Msys msys { get; set; }
    }

    public class Msys
    {
        public Message_Event message_event { get; set; }
    }

    public class Message_Event
    {
        public string type { get; set; }
        public string bounce_class { get; set; }
        public string campaign_id { get; set; }
        public string customer_id { get; set; }
        public string delv_method { get; set; }
        public string device_token { get; set; }
        public string error_code { get; set; }
        public string ip_address { get; set; }
        public string message_id { get; set; }
        public string msg_from { get; set; }
        public string msg_size { get; set; }
        public string num_retries { get; set; }
        public Rcpt_Meta rcpt_meta { get; set; }
        public string[] rcpt_tags { get; set; }
        public string rcpt_to { get; set; }
        public string rcpt_type { get; set; }
        public string raw_reason { get; set; }
        public string reason { get; set; }
        public string routing_domain { get; set; }
        public string subject { get; set; }
        public string template_id { get; set; }
        public string template_version { get; set; }
        public int timestamp { get; set; }
        public string transmission_id { get; set; }
    }

    public class Rcpt_Meta
    {
        public string customKey { get; set; }
    }
}

【讨论】:

  • 完成后如何提取字段的值?我需要将值放入 SQL 表中。
  • 非常感谢您的帮助。
  • @user2970010 没问题。您现在可以像 c# 中的任何其他类一样访问字段。请只接受其中一个帖子作为正确答案:)
【解决方案2】:

你走错路了。您希望能够不知道json 包含哪些字段,而只是知道它将是一个可以解析的有效json 对象。

我首先建议您了解 json 对象的真正含义以及它可以由什么组成。你可以阅读它here

接下来,您可以使用this 作为json 解析器。 关于使用的文档可以在here找到。

【讨论】:

  • 非常感谢您的帮助。
  • 当然。如果您接受了这个答案,您可以在以后将其标记为已接受。
【解决方案3】:

尝试反序列化为List&lt;Class1&gt;

var list = JsonConvert.DeserializeObject<List<Class1>>(json);

foreach (Message_Event msg in list.Select(c => c.msys.message_event))
{
    Console.WriteLine("campaign: " + msg.campaign_id);
    Console.WriteLine("from: " + msg.msg_from);
    Console.WriteLine("to: " + msg.rcpt_to);
    // etc.
}

小提琴:https://dotnetfiddle.net/Ei0apw

【讨论】:

  • 完成后如何提取字段的值?我需要将值放入 SQL 表中。
  • 这成功了。我不敢相信它是如此简单。非常感谢您的帮助。
【解决方案4】:

你可以使用:

public class Msys
{
    public List<Message_Event> message_event { get; set; }  
}

并用

初始化您的列表
System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Message_Event>(json);

【讨论】:

  • 您的对象类型必须与您的 json 类型相关
  • 例如:JSON Array = C# List
  • 非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2019-02-26
  • 2016-09-15
  • 2015-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多