I am writing a C# script in unity to get the weather of the location the player is in. I am almost done, all I have to do is extract the data from the JSON file the weather API returned. I am using SimpleJSON to read and extract the JSON. The JSON looks something like this:
{
"coord": {
"lon": (floating point number),
"lat": (floating point number)
},
"weather": [
{
"id": 804,
"main": "Clouds",
"description": "overcast clouds",
"icon": "04n"
}
],
"base": "stations",
"main": {
"temp": 292.01,
"feels_like": 292.12,
"temp_min": 291.15,
"temp_max": 293.15,
"pressure": 1018,
"humidity": 72
},
"visibility": 9000,
"wind": {
"speed": 1.5,
"deg": 130
},
"clouds": {
"all": 100
},
"dt": 1594239812,
"sys": {
"type": 1,
"id": 1308,
"country": "COUNTRY-CODE",
"sunrise": 1594178675,
"sunset": 1594235893
},
"timezone": 7200,
"id": 2954172,
"name": "CITY",
"cod": 200
}
I need to access the "description" in the "weather" array. However I could not figure out how to get it to work. This is my C# code:
var PARSED_JSON_2 = JSON.Parse(JSON_DATA_2);
var weather_description = PARSED_JSON_2["weather"][2]; //returns null
//I also tried something like this:
var weather_description = PARSED_JSON_2["weather"]["description"][2]; //returns null
var weather_description = PARSED_JSON_2["weather"]["description"][2].Value; //Returns nothing.
A Debug.Log would show an empty string.
I tried to follow this reference here: http://wiki.unity3d.com/index.php/SimpleJSON
(In a nutshell I need to access an element inside of a JSON array, but I can't figure out how to. Any help is appreciated.)
