I have some JSON from the EPA's UV Index API. The json is an array with separate elements in it. Example of the JSON:
[
{
"ORDER": 1,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 AM",
"UV_VALUE": 0
},
{
"ORDER": 2,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 AM",
"UV_VALUE": 1
},
{
"ORDER": 3,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 AM",
"UV_VALUE": 1
},
{
"ORDER": 4,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 AM",
"UV_VALUE": 2
},
{
"ORDER": 5,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 AM",
"UV_VALUE": 3
},
{
"ORDER": 6,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 PM",
"UV_VALUE": 7
},
{
"ORDER": 7,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 PM",
"UV_VALUE": 6
},
{
"ORDER": 8,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 PM",
"UV_VALUE": 6
},
{
"ORDER": 9,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 PM",
"UV_VALUE": 7
},
{
"ORDER": 10,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 04 PM",
"UV_VALUE": 5
},
{
"ORDER": 11,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 05 PM",
"UV_VALUE": 3
},
{
"ORDER": 12,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 06 PM",
"UV_VALUE": 1
},
{
"ORDER": 13,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 07 PM",
"UV_VALUE": 0
},
{
"ORDER": 14,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 08 PM",
"UV_VALUE": 0
},
{
"ORDER": 15,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 09 PM",
"UV_VALUE": 0
},
{
"ORDER": 16,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 10 PM",
"UV_VALUE": 0
},
{
"ORDER": 17,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 11 PM",
"UV_VALUE": 0
},
{
"ORDER": 18,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 12 AM",
"UV_VALUE": 0
},
{
"ORDER": 19,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 01 AM",
"UV_VALUE": 0
},
{
"ORDER": 20,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 02 AM",
"UV_VALUE": 0
},
{
"ORDER": 21,
"ZIP": 19021,
"DATE_TIME": "MAY/27/2015 03 AM",
"UV_VALUE": 0
}
]
In order to parse it, I assumed I could just use a foreach
statement to retrieve the value of the UV index property for each element. b.Result is the above JSON.
JObject EPAData = JObject.Parse(b.Result);
foreach (var UVIndex in EPAData.Root)
{
string uv = (string)UVIndex["UV_VALUE"];
//Do whatever I want with the UV index
};
However, that doesn't seem to be working. I get an error message:
Error in application: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
Your JSON represents an array, not an object, so use JArray.Parse
instead:
JArray EPAData = JArray.Parse(b.Result);
foreach (var UVIndex in EPAData)
{
Console.WriteLine (UVIndex["UV_VALUE"]); // 0, 1, 1, etc.
}
Example: https://dotnetfiddle.net/G4PkSf