Hi guys
I’ve try to get some api rest value from unity.
I’ve some problem because sometimes is impossible to deserialize the value inside json.
In this example i wouldlike to read location.name :
FROM JSON
"location": {
"name": "London",
"region": "City of London, Greater London",
"country": "United Kingdom",
"lat": 51.52,
"lon": -0.11,
"tz_id": "Europe/London",
"localtime_epoch": 1622795128,
"localtime": "2021-06-04 9:25"
},
this is the erroe when i try to get this value “ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index”.
When i debug jasonresponse i can see all value inside json file, but the problem is deserialize.
Any suggestion? THX
HttpWebRequest (HttpWebRequest)WebRequest.Create(String.Format("http://api.weatherapi.c"));
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string jsonResponse = reader.ReadToEnd();
WindMph info = JsonUtility.FromJson<WindMph>(jsonResponse);
Debug.Log(jsonResponse);
Debug.Log(info.location[0].name.ToString());
[System.Serializable]
public class WindMph
{
public List<Winddd> location;
}
[System.Serializable]
public class Winddd
{
public string name;
}
Moved from answer:
Ok, with this api i can read all weather value but without serialize all jsone, can take also the parameters as ID and name, but it’s impossible to access for example to coord, or main or wind…i’ve seen that with the it’s ok the list but only with { } it’s a problem.
{
"coord": {
"lon": -122.08,
"lat": 37.39
},
"weather": [
{
"id": 800,
"main": "Clear",
"description": "clear sky",
"icon": "01d"
}
],
"base": "stations",
"main": {
"temp": 282.55,
"feels_like": 281.86,
"temp_min": 280.37,
"temp_max": 284.26,
"pressure": 1023,
"humidity": 100
},
"visibility": 16093,
"wind": {
"speed": 1.5,
"deg": 350
},
"clouds": {
"all": 1
},
"dt": 1560350645,
"sys": {
"type": 1,
"id": 5122,
"message": 0.0139,
"country": "US",
"sunrise": 1560343627,
"sunset": 1560396563
},
"timezone": -25200,
"id": 420006353,
"name": "Mountain View",
"cod": 200
}