【问题标题】:Get child properties from JSON using Json.NET and C#使用 Json.NET 和 C# 从 JSON 获取子属性
【发布时间】:2018-02-01 22:17:17
【问题描述】:

我已经阅读了很多论坛,但似乎仍然无法将其拼凑在一起。我正在使用 Json.Net 来解析 JSON 文件。它非常大,我只想要一块。我拨打电话并像这样解析它:

string json = string.Empty;
HttpWebResponse response; 
string url = @""; 
HttpWebRequest request = (HttpWebRequest)(WebRequest.Create(url)); 

using (response = (HttpWebResponse)request.GetResponse()) 
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
        json = reader.ReadToEnd();
}

JObject obj = JObject.Parse(json);

就像我说的,这个 JSON 很大,但它只是一个很长的联系人列表。以下是单个联系人包含的内容:

{
"contacts": [
    {
        "addedAt": 1234,
        "vid": 1234,
        "canonical-vid": 1234,
        "merged-vids": [
            1234,
            1234
        ],
        "portal-id": 1234,
        "is-contact": true,
        "profile-token": "": "",
        "properties": {
            "country": {
                "value": "united states"
            },
            "firstname": {
                "value": "Example"
            },
            "jobtitle": {
                "value": "Developer"
            },
            "primary_role": {
                "value": "Developer"
            },
            "lastmodifieddate": {
                "value": "1234"
            },
            "state": {
                "value": "CO"
            },
            "email": {
                "value": "example@notreal.com"
            },
            "lastname": {
                "value": "Example"
            }
        },
        "form-submissions": [
            {
                "conversion-id": "1234",
                "timestamp": 1234,
                "form-id": "",
                "portal-id": 1234,
                "page-url": "",
                "page-title": "",
                "title": "",
                "form-type": "",
                "meta-data": []
            }
        ],
        "identity-profiles": [
            {
                "vid": 1234,
                "saved-at-timestamp": 1234,
                "deleted-changed-timestamp": 234,
                "identities": [
                    {
                        "type": "EMAIL",
                        "value": "example@notreal.com",
                        "timestamp": 1243,
                        "is-primary": true
                    },
                    {
                        "type": "TYPE",
                        "value": "",
                        "timestamp": 1234
                    },
                    {
                        "type": "EMAIL",
                        "value": "example@notreal.com",
                        "timestamp": 1234,
                        "is-secondary": true
                    }
                ]
            },
            {
                "vid": 1234,
                "saved-at-timestamp": 1324,
                "deleted-changed-timestamp": 0,
                "identities": [
                    {
                        "type": "TYPE",
                        "value": "",
                        "timestamp": 1234
                    }
                ]
            }
        ],
        "merge-audits": [
            {
                "canonical-vid": 1234,
                "vid-to-merge": 1234,
                "timestamp": 1324,
                "entity-id": "",
                "user-id": 1234,
                "num-properties-moved": 234,
                "merged_from_email": {
                    "value": "notreal@example.com",
                    "source-type": "CONTACTS",
                    "source-id": "SOURCE",
                    "source-label": null,
                    "source-vids": [
                        1234
                    ],
                    "timestamp": 1234,
                    "selected": false
                },
                "merged_to_email": {
                    "value": "example@notreal.com",
                    "source-type": "TYPE",
                    "source-id": "",
                    "source-label": null,
                    "timestamp": 1234,
                    "selected": false
                },
                "first-name": "Example",
                "last-name": "Example"
            }
        ]
    }
],
"has-more": true,
"vid-offset": 1234
}

我不关心这里的任何信息,除了“属性”下的信息。我需要将属性中的所有内容存储在字典中,例如 。下面的代码为我提供了整个“属性”卡盘:

foreach(var item in contacts.Children())
        {
            var itemProps = item.Children<JProperty>();
            var myElement = itemProps.FirstOrDefault(x => x.Name == "properties");
            var myElementValue = myElement.Value;
        }

我正在努力解决如何进一步缩小范围并获取字典或填充属性内键值的东西?任何帮助将不胜感激。

【问题讨论】:

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


【解决方案1】:

根据您的描述,您的 myElement 是一个 JProperty 和嵌套的 JObjects,每个都有一个 JProperty 子级,如下所示:

这有点难看,您需要添加一些空检查等,但您可以遍历元素并填充属性字典,如下所示:

var myElement = o.Children<JProperty>().FirstOrDefault(x => x.Name == "properties");
var jobj = (JObject)myElement.Value;

Dictionary<string, string> myProperties = new Dictionary<string, string>();

foreach(var jprop in jobj) {

    var  key = jprop.Key;
    var value = ((JObject)jprop.Value).Properties().First().Value.ToString();
    myProperties[key] = value;      
}

【讨论】:

  • 非常有帮助!感谢您对正在发生的事情进行可视化,这真的帮助我理解了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多