如何从JSON获取数据并转换为对象?

时间:2017-06-06 21:00:52

标签: c# json json.net

我正在尝试从请求响应中接收数据,这是JSON,如下所示:

  

{   “CNT”:1,   “清单”:[{       “坐标”:{           “LON”:18.55,           “LAT”:50.11       },       “SYS”:{           “类型”:1,           “ID”:5356,           “消息”:0.0095,           “国”: “PL”,           “日出”:1496630290,           “夕阳”:1496688673       },       “天气”:[{           “ID”:800,           “主”:“清除”,           “描述”:“晴朗的天空”,           “图标”: “01D”       }],       “主要”:{           “温度”:293.71,           “压力”:1014,           “湿度”:42,           “temp_min”:293.15,           “temp_max”:294.15       },       “可见性”:10000,       “风”:{           “速度”:4.1,           “度”:60       },       “云”:{           “全”:0       },       “DT”:1496686603,       “ID”:7531758,       “名”:“雷布尼克”   }]   }

我正在尝试使用JSON.NET反序列化JSON并接收数据。但我不确切知道如何正确地做到这一点。 我试图通过使用我的类方法实现这一点:

    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;
    }

但它似乎不起作用。在这种情况下,我尝试从JSON中的“main”属性接收数据并转换为我的类对象:

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; }
}

你能解释一下,它应该如何运作以及我的错在哪里?

1 个答案:

答案 0 :(得分:3)

最初,您需要声明以下类:

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; }
}

然后你可以反序列化你的json,如下所示:

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

其中jsonString是您的JSON。

PS。我使用jsonutils和你的json字符串派生了上面的类。