This should be really simple, I think I am just missing the obvious, so I am asking for some help. I have a json file that I need to grab some subkeys and put them in an array. My code will show the subkeys, but it also shows each subkeys values and child subkeys. I don't want it to be recursive, but cannot figure out how to do this. I've included a sample json array and the code I am using.
Here is the JSON array that I need to parse.
I would like to only parse the subkeys Cold and Hot, without any extra keys or values.
Dim jsonString As String = "
{
""Date"": ""2019-08-01T00:00:00"",
""Temperature"": 25,
""Summary"": ""Hot"",
""DatesAvailable"": [
""2019-08-01T00:00:00"",
""2019-08-02T00:00:00""
],
""TemperatureRanges"": {
""Cold"": {
""High"": 20,
""Low"": -10
},
""Hot"": {
""High"": 60,
""Low"": 20
}
}
}
"
Here is my VB.net code
Dim forecastNode As JsonNode = JsonNode.Parse(jsonString)
Dim temperatureNode As JsonNode = forecastNode("TemperatureRanges")
Console.WriteLine(temperatureNode.ToJsonString())
This is my result:
{"Cold":{"High":20,"Low":-10},"Hot":{"High":60,"Low":20}}
This is my desired result:
Cold, Hot
Any suggestions on how to accomplish this would be appricated.
Try this:
Dim forecastNode As JsonNode = JsonNode.Parse(jsonString)
Dim temperatureNode As JsonObject = forecastNode("TemperatureRanges")
Dim subNode As KeyValuePair(Of String, JsonNode)
For Each subNode In temperatureNode
Console.WriteLine(subNode.Key)
Next
Output is
Cold
Hot
(I leave it as an exercise to you to format the output the way you like).