Reputation: 2029
I have a nested json format like given below,I need to read all the nodes till the last node to get the node name, node value, attribute name, parent node. I tried using a recursive function to read data but its not working correctly. Please help with a simple solution to read data. (precisely a dynamic one which can handle any number of nodes and attributes)
Required Output format
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 Input format
{
"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"
}
]
}
Upvotes: 1
Views: 4875
Reputation: 14624
You can use Json.NET to deserialize the JSON into the following class:
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; }
}
and you can use this method to print all the values in the required output format using a console application:
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);
}
}
Let's say you put the JSON to a string variable named jsonInput
, here's how you deserialize and print all of its values:
string jsonInput = ...; // put the above json here
JsonNodes nodes = JsonConvert.DeserializeObject<JsonNodes>(jsonInput);
PrintValues(nodes, string.Empty);
Console.ReadLine();
Upvotes: 3
Reputation: 17865
Use the following method to enumerate your JSON string using Json.NET:
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);
}
}
You can call it like this (if jsonString
is your input):
var jsonObject = JObject.Parse(jsonString);
JsonVisitor(jsonObject.Root);
Upvotes: 0