【问题标题】:Convert JSON tree structure to list将 JSON 树结构转换为列表
【发布时间】:2018-01-09 11:54:35
【问题描述】:

我是 JSON 新手。我在树结构中有一个 JSON 列表,如下所示:

{
  "complaint@simulator.amazonses.com": {
    "time": "2018-01-02T20:45:46.65Z",
    "type": "Complaint",
    "bounceType": "null",
    "bounceSubType": "null"
  },
  "struax@example.org": {
    "time": "2018-01-02T20:53:03Z",
    "type": "Bounce",
    "bounceType": "Permanent",
    "bounceSubType": "Suppressed"
  },
  "bounce-test@service.socketlabs.com": {
    "time": "2018-01-02T21:06:40.097Z",
    "type": "Bounce",
    "bounceType": "Permanent",
    "bounceSubType": "Suppressed"
  },
  "bounce@simulator.amazonses.com": {
    "time": "2018-01-02T21:08:02Z",
    "type": "Bounce",
    "bounceType": "Permanent",
    "bounceSubType": "General"
  },
  "jstrechay@example.org": {
    "time": "2018-01-05T06:31:39Z",
    "type": "Bounce",
    "bounceType": "Permanent",
    "bounceSubType": "General"
  },
  "leematt45@example.org": {
    "time": "2018-01-05T06:49:13Z",
    "type": "Bounce",
    "bounceType": "Permanent",
    "bounceSubType": "Suppressed"
  },
  "afbweb@example.org": {
    "time": "2018-01-07T12:50:38Z",
    "type": "Bounce",
    "bounceType": "Transient",
    "bounceSubType": "General"
  },
  "bajanina2013@example.org": {
    "time": "2018-01-02T08:12:19Z",
    "type": "Bounce",
    "bounceType": "Transient",
    "bounceSubType": "MailboxFull"
  },
  "martin.bunt@example.org": {
    "time": "2018-01-05T07:00:24Z",
    "type": "Complaint",
    "bounceType": "null",
    "bounceSubType": "null"
  }
}

我的 SQL 表列是EmailtimetypebounceTypebounceSubType。 如何从 JSON 列表中提取数据并将其保存在数据库中?

我正在使用此代码:

string JSON = response.Content.ReadAsStringAsync().Result;
var jObj = (JObject)JsonConvert.DeserializeObject(JSON);

JSON 是树形结构,我无法获取列表中的父节点和相应的子节点。

【问题讨论】:

标签: c# asp.net json json.net


【解决方案1】:

这就是我要做的。首先,定义一个模型类来保存项目数据(您可能已经有这样的类):

class BounceItem
{
    public string Email { get; set; }
    public DateTime Time { get; set; }
    public string Type { get; set; }
    public string BounceType { get; set; }
    public string BounceSubType { get; set; }
}

接下来,将 JSON 反序列化为 Dictionary<string, BounceItem>

var dict = JsonConvert.DeserializeObject<Dictionary<string, BounceItem>>(json);

电子邮件地址将成为字典中的键,值将是包含嵌套属性的 BounceItem 对象。但是请注意,此时不会填充每个 BounceItem 中的 Email 属性。

要解决这个问题,对字典进行后处理,将每个键复制到相应项目的 Email 属性中,并将结果存储到 List&lt;BounceItem&gt;

var list = dict.Select(kvp => { kvp.Value.Email = kvp.Key; return kvp.Value; }).ToList();

现在您有了一个应该与您的 SQL 表匹配的模型对象列表,您可以使用适合您选择的数据库的任何方法插入它们。

小提琴:https://dotnetfiddle.net/5rzyCs

【讨论】:

    【解决方案2】:

    如果您使用的是 c#.NET,则使用 Json.NET 或使用 Newtonsoft.Json.Linq 非常简单。

    第一种方法:

    dynamic jsonObject = JsonConvert.DeserializeObject("your json string");
    

    第二种方法:

    dynamic jsonObject = JObject.Parse("your json string");
    

    解析后可以将jsonObject传递给DB。

    【讨论】:

    • 感谢回复@Arun 我正在使用此代码字符串 JSON = response.Content.ReadAsStringAsync().Result; var jObj = (JObject)JsonConvert.DeserializeObject(JSON); json 是树形结构,我无法在列表中获取父节点和受尊敬的子节点
    【解决方案3】:

    您需要根据您的Json 响应创建一个类。比从那个类反序列化你的对象像这样

    Class1 class1Object = JsonConvert.DeserializeObject<Class1>(str)
    

    【讨论】:

    • 感谢回复@shyam 我正在使用此代码字符串 JSON = response.Content.ReadAsStringAsync().Result; var jObj = (JObject)JsonConvert.DeserializeObject(JSON); json 是树形结构,我无法在列表中获取父节点和受尊敬的子节点
    • 如果你没有通过任何课程,它将采取动态的。 jObj = (JObject)JsonConvert.DeserializeObject(JSON);实际上是 jObj = (JObject)JsonConvert.DeserializeObject(JSON);所以您需要创建一个与您的 json 对象具有相同模式的类并传递该类而不是动态的
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 2022-07-15
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多