ZZnOB ZZnOB - 27 days ago 6
C# Question

Parse JSON in C#

I have JSON file like this

{
"Value": {
"111": {
"TZ": 0,
"AllowChange": 3,
"InCall": true,
"OutCall": true,
"BeginDT": "00:00:00",
"EndDT": "23:59:59",
"Days": 127
},
"222": {
"TZ": 0,
"AllowChange": 3,
"InCall": true,
"OutCall": true,
"BeginDT": "00:00:00",
"EndDT": "23:59:59",
"Days": 127
},
"333": {
"TZ": 0,
"AllowChange": 3,
"InCall": true,
"OutCall": true,
"BeginDT": "00:00:00",
"EndDT": "23:59:59",
"Days": 127
},
"444": {
"TZ": 0,
"AllowChange": 3,
"InCall": true,
"OutCall": true,
"BeginDT": "00:00:00",
"EndDT": "23:59:59",
"Days": 127
},
"555": {
"TZ": 0,
"AllowChange": 3,
"InCall": true,
"OutCall": true,
"BeginDT": "00:00:00",
"EndDT": "23:59:59",
"Days": 127
},
},
"Success": true,
"Error": null,
"Failure": false
}


how i can get
"111","222","333","444","555"
?

Answer

I just used Newtonsoft.Json and was able to read your json with the following code:

string json = File.ReadAllText(pathToFile);
dynamic jsonObj = JsonConvert.DeserializeObject(json);
dynamic value = jsonObj["Value"];

foreach (var val in value)
{
     Console.WriteLine(val.Name);
}

You will need to include Microsoft.CSharp as a reference to your project for this to compile.

Comments