我有一个 JSON 数据集并使用 C# JSON.net 库我试图将数据读取为连接字符串,但无法提取数据。数据有根元素,然后是行。对于“行”中的每一行,我想提取 ["conversionPathValue"]["nodeValue"] 值的列表并将它们连接在一起,然后将它们与原始值值连接。2行的示例代码如下:
"rows": [
[
{
"conversionPathValue": [
{
"interactionType": "CLICK",
"nodeValue": "MET"
}
]
},
{
"primitiveValue": "20130122"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "000001"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "11"
},
{
"primitiveValue": "7290.521799"
}
],
[
{
"conversionPathValue": [
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"interactionType": "CLICK",
"nodeValue": "MET"
},
{
"nodeValue": "(none)"
},
{
"nodeValue": "(none)"
},
{
"interactionType": "CLICK",
"nodeValue": "organic"
}
]
},
{
"primitiveValue": "20130122"
},
{
"primitiveValue": "000"
},
{
"primitiveValue": "000011"
},
{
"primitiveValue": "005"
},
{
"primitiveValue": "1"
},
{
"primitiveValue": "1628.0"
}
],
.....etc........
使用以下代码:(jsonExtract 是一个 JObject)
var rows = jsonExtract["root"]["rows"][0].Children();
foreach (JToken result in rows)
{
var primitiveValues = result["primitiveValue"].Values<string>();
var pathValues = result["conversionPathValue"].Values<string>();
string joinedprimitiveValues = string.Join(",", primitiveValues);
string joinedpathValues = string.Join("-", pathValues);
file2.WriteLine(joinedpathValues + ", " + joinedprimitiveValues);
}
这会在设置primitiveValues 时出现“对象引用未设置为对象的实例”的错误。
我知道问题可能出在嵌套元素上,但我不知道如何解决这些问题。有人可以帮忙吗?