【发布时间】: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;
}
我正在努力解决如何进一步缩小范围并获取字典或填充属性内键值的东西?任何帮助将不胜感激。
【问题讨论】:
-
Properties 是复数,这意味着它由多个值组成——根据你的帖子,有 8 个——所以如果你要解析它,你需要找出每个值
-
我们需要知道 JSON 文件的格式才能给出具体答案,即minimal reproducible example。但是已经有几个关于解析巨大的 JSON 文件而不将它们完全加载到内存中的问题,请参阅 Deserialize json array stream one item at a time 和 System.OutOfMemoryException with JSON.NET with
List<object>。但是使用JObject.Parse(json);加载它会最大化内存使用,因为您将为 JSON 分配一个巨大的字符串,然后为所有属性分配JProperty字符串名称。
标签: c# .net json parsing json.net