从嵌套的Json Object获取值

时间:2014-02-01 09:56:41

标签: c# json

我有一个嵌套的json格式,如下所示,我需要读取所有节点,直到最后一个节点获取节点名称,节点值,属性名称,父节点。我尝试使用递归函数来读取数据,但它无法正常工作。请帮助一个简单的解决方案来读取数据。 (正是一个可以处理任意数量的节点和属性的动态的)

必需的输出格式

   name     |   Value        | IsNode | Parent
------------------------------------------------
 updated    2014-01-01  false   record
 position   ambassador  true    record/person
 first_name Eliyahu     true    record/person/names
 alias      Eli         true    record/person/names/aliases
 country    ISRAEL      true    record/details/countries
 category   DIPLOMAT    false   record

Json输入格式

{
  "Node": "record",
  "NodeValue": null,
  "ParentNode": "records",
  "Nodes": [
    {
      "Node": "person",
      "NodeValue": null,
      "ParentNode": "record",
      "Nodes": [
        {
          "Node": "title",
          "NodeValue": "Dr",
          "ParentNode": "person",
          "Nodes": [

          ],
          "Attributes": [

          ]
        },
        {
          "Node": "position",
          "NodeValue": "Ambassador",
          "ParentNode": "person",
          "Nodes": [

          ],
          "Attributes": [

          ]
        },
        {
          "Node": "names",
          "NodeValue": null,
          "ParentNode": "person",
          "Nodes": [
            {
              "Node": "first_name",
              "NodeValue": "Eliyahu",
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [

              ]
            },
            {
              "Node": "last_name",
              "NodeValue": "BEN TURA",
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [

              ]
            },
            {
              "Node": "aliases",
              "NodeValue": null,
              "ParentNode": "names",
              "Nodes": [
                {
                  "Node": "alias",
                  "NodeValue": "BEN TURA,Eli",
                  "ParentNode": "aliases",
                  "Nodes": [

                  ],
                  "Attributes": [

                  ]
                }
              ],
              "Attributes": [

              ]
            },
            {
              "Node": "alternative_spelling",
              "NodeValue": null,
              "ParentNode": "names",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "agedata",
          "NodeValue": null,
          "ParentNode": "person",
          "Nodes": [
            {
              "Node": "age",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "as_of_date",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "dob",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            },
            {
              "Node": "deceased",
              "NodeValue": null,
              "ParentNode": "agedata",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        }
      ],
      "Attributes": [
        {
          "Key": "ssn",
          "Value": ""
        },
        {
          "Key": "e-i",
          "Value": "M"
        }
      ]
    },
    {
      "Node": "details",
      "NodeValue": null,
      "ParentNode": "record",
      "Nodes": [
        {
          "Node": "passports",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "passport",
              "NodeValue": null,
              "ParentNode": "passports",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "country",
                  "Value": ""
                },
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "place_of_birth",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [

          ],
          "Attributes": [
            {
              "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
              "Value": "true"
            }
          ]
        },
        {
          "Node": "locations",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "location",
              "NodeValue": null,
              "ParentNode": "locations",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "country",
                  "Value": "SENEGAL"
                },
                {
                  "Key": "city",
                  "Value": "Dakar"
                },
                {
                  "Key": "state",
                  "Value": "Dakar"
                },
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "countries",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "country",
              "NodeValue": "ISRAEL",
              "ParentNode": "countries",
              "Nodes": [

              ],
              "Attributes": [

              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "companies",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "company",
              "NodeValue": null,
              "ParentNode": "companies",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        },
        {
          "Node": "keywords",
          "NodeValue": null,
          "ParentNode": "details",
          "Nodes": [
            {
              "Node": "keyword",
              "NodeValue": null,
              "ParentNode": "keywords",
              "Nodes": [

              ],
              "Attributes": [
                {
                  "Key": "{http://www.w3.org/2001/XMLSchema-instance}nil",
                  "Value": "true"
                }
              ]
            }
          ],
          "Attributes": [

          ]
        }
      ],
      "Attributes": [

      ]
    }
  ],
  "Attributes": [
    {
      "Key": "category",
      "Value": "DIPLOMAT"
    },
    {
      "Key": "editor",
      "Value": ""
    },
    {
      "Key": "entered",
      "Value": "2010-11-19"
    },
    {
      "Key": "sub-category",
      "Value": "PEP"
    },
    {
      "Key": "uid",
      "Value": "1389120"
    },
    {
      "Key": "updated",
      "Value": "2014-01-01"
    }
  ]
}

2 个答案:

答案 0 :(得分:3)

您可以使用Json.NET将JSON反序列化为以下类:

public class JsonNodes
{
    public string Node { get; set; }
    public string NodeValue { get; set; }
    public string ParentNode { get; set; }
    public List<JsonNodes> Nodes { get; set; }
    public List<JsonNodesAttribute> Attributes { get; set; }
}

public class JsonNodesAttribute
{
    public string Key { get; set; }
    public string Value { get; set; }
}

您可以使用此方法使用控制台应用程序以所需的输出格式打印所有值:

private static void PrintValues(JsonNodes nodes, string parent)
{
    Console.WriteLine(string.Format("Name: {0}", nodes.Node));
    Console.WriteLine(string.Format("Value: {0}", nodes.NodeValue));
    Console.WriteLine("IsNode: true");
    Console.WriteLine(string.Format("Parent: {0}", parent)); 
    Console.WriteLine();

    if (parent == string.Empty)
    {
        parent += nodes.Node;
    }
    else
    {
        parent += string.Format("/{0}", nodes.Node);
    }

    foreach (JsonNodesAttribute attribute in nodes.Attributes)
    {
        Console.WriteLine(string.Format("Name: {0}", attribute.Key));
        Console.WriteLine(string.Format("Value: {0}", attribute.Value));
        Console.WriteLine("IsNode: false");
        Console.WriteLine(string.Format("Parent: {0}", parent));
    }

    Console.WriteLine();

    foreach (JsonNodes childNode in nodes.Nodes)
    {
        PrintValues(childNode, parent);
    }
}

假设您将JSON放到名为jsonInput的字符串变量中,以下是反序列化和打印其所有值的方法:

string jsonInput = ...; // put the above json here
JsonNodes nodes = JsonConvert.DeserializeObject<JsonNodes>(jsonInput);
PrintValues(nodes, string.Empty);

Console.ReadLine();

答案 1 :(得分:0)

使用以下方法使用Json.NET枚举您的JSON字符串:

void JsonVisitor(JToken node, int indent = 0)
{
    var indentString = new String(' ', indent * 4);
    Console.WriteLine("{0}+{1}: {2}", indentString, node["Node"], node["NodeValue"]);
    foreach (var attribute in node["Attributes"])
    {
        Console.WriteLine("{0} {1}: {2}", indentString, attribute["Key"], attribute["Value"]);
    }
    foreach (var subNode in node["Nodes"])
    {
        JsonVisitor(subNode, indent + 1);
    }
}

您可以这样调用它(如果jsonString是您的输入):

var jsonObject = JObject.Parse(jsonString);
JsonVisitor(jsonObject.Root);
相关问题