- Home /
Problem deserialize Json api rest
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
}
Answer by xxmariofer · Jun 04, 2021 at 09:54 AM
I have never tried serializing one property and i dont know if its posible or how it works, my suggestion would be serializing the fulljson
[System.Serializable]
public class Location
{
public List location;
}
[System.Serializable]
public class Winddd
{
public string name;
public string region;
public string country;
public float lat;
public float lon;
public string tz_id;
public float localtime_epoch;
public string localtime;
}
Debug.Log(info.location[0].name);
Your answer
Follow this Question
Related Questions
PATCH and DELETE methods - HTTP Requests problem 0 Answers
UnityWebRequest.Post with MultipartFormDataSection returns Bad Request (400) 0 Answers
Posting JSON data inside a form post urlencodes the json data 0 Answers
Json Deserialize to dictionary[Solved] 1 Answer
Can anyone recommend a json parser good for using with a web api? 0 Answers