I'm trying to receive data from request response which is JSON like this:
{ "cnt":1, "list":[{ "coord":{ "lon":18.55, "lat":50.11 }, "sys":{ "type":1, "id":5356, "message":0.0095, "country":"PL", "sunrise":1496630290, "sunset":1496688673 }, "weather":[{ "id":800, "main":"Clear", "description":"clear sky", "icon":"01d" }], "main":{ "temp":293.71, "pressure":1014, "humidity":42, "temp_min":293.15, "temp_max":294.15 }, "visibility":10000, "wind":{ "speed":4.1, "deg":60 }, "clouds":{ "all":0 }, "dt":1496686603, "id":7531758, "name":"Rybnik" }] }
I'm trying to use JSON.NET to deserialize JSON and receive data. But I don't know exactly how to do it properly. I tried to achieve this by using my class method:
public Rootobject DeserializeJSON()
{
private JObject responseObject;
private JToken responseToken;
private JArray responseArray;
responseObject = JObject.Parse(json);
Rootobject Weather = new Rootobject();
responseArray = (JArray)responseObject.SelectToken("list");
responseToken = (JToken)responseArray.SelectToken("main");
Weather = responseToken.ToObject<Rootobject>();
return Weather;
}
But It doesn't seems to work. In this case, I tried to receive data from "main" property in JSON and convert in to my class object:
class Main
{
public float temp { get; set; }
public float pressure { get; set; }
public float humidity { get; set; }
public float temp_min { get; set; }
public float temp_max { get; set; }
}
Could you explain me, how it should works and where's my fault, please?