C #: fetching / fetching a child of a node from a JSON structure

How can we extract or get the values โ€‹โ€‹of child nodes from a JSON structure in C #.

my application uses OpenWeatherMap , I need to get the name from the city temp from the list and description from the weather , my JSON and Class structure is below

{
  "cod": "200",
  "message": 0.0284,
  "city": {
    "id": 2643743,
    "name": "London",
    "coord": {
      "lon": -0.12574,
      "lat": 51.50853
    },
    "country": "GB",
    "population": 0,
    "sys": {
      "population": 0
     }
  },
  "cnt": 1,
  "list": [
    {
      "dt": 1429268400,
      "temp": {
        "day": 12.21,
        "min": 4.86,
        "max": 13.18,
        "night": 4.86,
        "eve": 11.76,
        "morn": 12.21
      },
      "pressure": 1028.8,
      "humidity": 66,
      "weather": [
         {
           "id": 803,
           "main": "Clouds",
           "description": "broken clouds",
           "icon": "04d"
        }
      ],
      "speed": 5.9,
      "deg": 67,
      "clouds": 80
    }
  ]
}

C # class

public class WeatherForeCast
{
    public string City { get; set; }
    public decimal Day { get; set; }
    public decimal Min { get; set; }
    public decimal Max { get; set; }
    public decimal Night { get; set; }
    public string Description { get; set; }
}

So far, I am familiar with using JSON.net to serialize and deserialize C # objects for JSON, which has the same structure.

+4
source share
2 answers

WeatherForecast, SelectToken JObject:

var parsed = JObject.Parse(json);
var forecast = new WeatherForeCast();

forecast.City = parsed.SelectToken("city.name").Value<string>();
forecast.Day = parsed.SelectToken("list[0].temp.day").Value<decimal>();
forecast.Description = parsed.SelectToken("list[0].weather[0].description").Value<string>();
forecast.Min = parsed.SelectToken("list[0].temp.min").Value<decimal>();
forecast.Max = parsed.SelectToken("list[0].temp.max").Value<decimal>();
forecast.Night = parsed.SelectToken("list[0].temp.night").Value<decimal>();

, , JSON. JSON , SelectToken , .

+10

json2csharp.com .

public class Coord
{
    public double lon { get; set; }
    public double lat { get; set; }
}

public class Sys
{
    public int population { get; set; }
}

public class City
{
    public int id { get; set; }
    public string name { get; set; }
    public Coord coord { get; set; }
    public string country { get; set; }
    public int population { get; set; }
    public Sys sys { get; set; }
}

public class Temp
{
    public double day { get; set; }
    public double min { get; set; }
    public double max { get; set; }
    public double night { get; set; }
    public double eve { get; set; }
    public double morn { get; set; }
}

public class Weather
{
    public int id { get; set; }
    public string main { get; set; }
    public string description { get; set; }
    public string icon { get; set; }
}

public class List
{
    public int dt { get; set; }
    public Temp temp { get; set; }
    public double pressure { get; set; }
    public int humidity { get; set; }
    public List<Weather> weather { get; set; }
    public double speed { get; set; }
    public int deg { get; set; }
    public int clouds { get; set; }
}

public class RootObject
{
    public string cod { get; set; }
    public double message { get; set; }
    public City city { get; set; }
    public int cnt { get; set; }
    public List<List> list { get; set; }
}

JSON.NET .

var jsonObject = JsonConvert.DeserializeObject<RootObject>(jsonString);

RootObject, , .

0

All Articles