1

我有一个 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 时出现“对象引用未设置为对象的实例”的错误。

我知道问题可能出在嵌套元素上,但我不知道如何解决这些问题。有人可以帮忙吗?

4

1 回答 1

3

我认为您在代码中遇到的问题是您没有处理primitiveValuesorpathValues为 null 的情况,由于您处理行的方式,这实际上会在每次迭代中发生。

例如,如果这是一行:

{
    "primitiveValue": "20130122"
},

然后result["conversionPathValue"]就要回去了null

试试这个:

var obj = JObject.Parse(json);
var rows = obj["rows"].Children();

foreach (JToken row in rows)
{
    List<string> conversionPaths = new List<string>();
    List<string> primitiveValues = new List<string>();

    foreach (JToken value in row.Children())
    {
        var primitiveValue = value["primitiveValue"];

        if (primitiveValue != null)
        {
            primitiveValues.Add(primitiveValue.Value<string>());
        }

        var conversionPathValue = value["conversionPathValue"];

        if (conversionPathValue != null)
        {
            var paths = conversionPathValue.Children()
                .Where(t => t["nodeValue"] != null)
                .Select(t => t["nodeValue"].Value<string>());

            conversionPaths.AddRange(paths);
        }
    }

    Console.WriteLine("Primitive Values:");
    Console.WriteLine(string.Join(",", primitiveValues.ToArray()));
    Console.WriteLine("Conversion Paths:");
    Console.WriteLine(string.Join(",", conversionPaths.ToArray()) + "\n");
}

当我在给定的 JSON 上运行它时,它会产生以下内容:

Primitive Values:
20130122,000,000001,000,11,7290.521799
Conversion Paths:
MET

Primitive Values:
20130122,000,000011,005,1,1628.0
Conversion Paths:
MET,MET,MET,MET,MET,MET,MET,MET,(none),(none),organic

请注意,我添加{}围绕 JSON 使其工作。

于 2013-02-11T20:24:41.543 回答