I have a json file in this format:
{
"3874D632": {
"FirstName": "Jack",
"LastName": "Andersen"
},
"34F43A33": {
"FirstName": "John",
"LastName": "Smith"
}
}
StreamReader read_json = new StreamReader(path_json_file);
json = JSONNode.Parse(read_json.ReadToEnd());
read_json.Close();
how to obtain 3874D632 or 34F43A33 ??
json[????].
Note: this answer refers to the SimpleJSON library posted on the UnifyWiki site, which is not to be confused with the completely different SimpleJson library that ships as part of RestSharp.
If the JSONNode
represents a JSON object, you can cast it to JSONObject
and from there you can enumerate the key-value pairs like a dictionary. The Key
of each key-value pair will have the value you seek. See example below:
string json = @"
{
""3874D632"": {
""FirstName"": ""Jack"",
""LastName"": ""Andersen""
},
""34F43A33"": {
""FirstName"": ""John"",
""LastName"": ""Smith""
}
}";
var node = JSONNode.Parse(json);
if (node.Tag == JSONNodeType.Object)
{
foreach (KeyValuePair<string, JSONNode> kvp in (JSONObject)node)
{
Console.WriteLine(string.Format("{0}: {1} {2}",
kvp.Key, kvp.Value["FirstName"].Value, kvp.Value["LastName"].Value));
}
}
Output:
3874D632: Jack Andersen
34F43A33: John Smith