0

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?

Jakub Nowacki
  • 121
  • 2
  • 11
  • 1
    There's like a bizillion duplicate questions on SO regarding this not to mention a lovely help page on json.net. No need for custom methods. –  Jun 06 '17 at 21:07
  • Yeah, you're right. I was trying to find solution first, before asking... but I was not trying enough propably. Sorry for duplicating. I'll be more patient next time. – Jakub Nowacki Jun 06 '17 at 21:22

1 Answers1

2

Initially, you need to declare the following classes:

public class Coord
{
    [JsonProperty("lon")]
    public double Lon { get; set; }

    [JsonProperty("lat")]
    public double Lat { get; set; }
}

public class Sys
{
    [JsonProperty("type")]
    public int Type { get; set; }

    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("message")]
    public double Message { get; set; }

    [JsonProperty("country")]
    public string Country { get; set; }

    [JsonProperty("sunrise")]
    public int Sunrise { get; set; }

    [JsonProperty("sunset")]
    public int Sunset { get; set; }
}

public class Weather
{
    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("main")]
    public string Main { get; set; }

    [JsonProperty("description")]
    public string Description { get; set; }

    [JsonProperty("icon")]
    public string Icon { get; set; }
}

public class Main
{
    [JsonProperty("temp")]
    public double Temp { get; set; }

    [JsonProperty("pressure")]
    public int Pressure { get; set; }

    [JsonProperty("humidity")]
    public int Humidity { get; set; }

    [JsonProperty("temp_min")]
    public double TempMin { get; set; }

    [JsonProperty("temp_max")]
    public double TempMax { get; set; }
}

public class Wind
{

    [JsonProperty("speed")]
    public double Speed { get; set; }

    [JsonProperty("deg")]
    public int Deg { get; set; }
}

public class Clouds
{

    [JsonProperty("all")]
    public int All { get; set; }
}

public class List
{
    [JsonProperty("coord")]
    public Coord Coord { get; set; }

    [JsonProperty("sys")]
    public Sys Sys { get; set; }

    [JsonProperty("weather")]
    public IList<Weather> Weather { get; set; }

    [JsonProperty("main")]
    public Main Main { get; set; }

    [JsonProperty("visibility")]
    public int Visibility { get; set; }

    [JsonProperty("wind")]
    public Wind Wind { get; set; }

    [JsonProperty("clouds")]
    public Clouds Clouds { get; set; }

    [JsonProperty("dt")]
    public int Dt { get; set; }

    [JsonProperty("id")]
    public int Id { get; set; }

    [JsonProperty("name")]
    public string Name { get; set; }
}

public class Example
{

    [JsonProperty("cnt")]
    public int Cnt { get; set; }

    [JsonProperty("list")]
    public IList<List> List { get; set; }
}

Then you can deserialize your json as below:

var example = JsonConvert.DeserializeObject<Example>(jsonString)

where jsonString is your JSON.

PS. I derived the above classes using jsonutils and your json string.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • @Downvoter I would honestly appreaciate a reasoning about your downvote. Thanks in advance – Christos Jun 06 '17 at 21:11
  • I was thinking about that. But I don't need all of this data. I exactly need Main and Name property. Does it makes sense to declare all of those classes anyway? – Jakub Nowacki Jun 06 '17 at 21:12
  • So https://jsonutils.com/ will automatically pascal-case the properties and optionally add `[JsonProperty]` attributes? Thanks - I hadn't seen that site before. – dbc Jun 06 '17 at 21:13
  • 1
    @JakubNowacki I think that this way makes sense, since you define the whole structure of your json and later on if you need to use any other property you would not have to do any change in your code :) You would just read it ! – Christos Jun 06 '17 at 21:15
  • @dbc Yeap, it's a really cool tool that I learned about it the last couple of weeks by a colleague :) You are welcome – Christos Jun 06 '17 at 21:16
  • I didn't downvote, but if the question is a duplicate to a question that already has answers, that may explain the downvote. – hatchet - done with SOverflow Jun 06 '17 at 21:17
  • @Christos I tried to avoid that, but I see your point. It makes sense. jsonutils.com will be very useful too for me :) Thank you very much ;) – Jakub Nowacki Jun 06 '17 at 21:20
  • @JakubNowacki You are welcome :) – Christos Jun 06 '17 at 21:21