【问题标题】:How to get data from JSON and convert into the object? [duplicate]如何从 JSON 中获取数据并转换为对象? [复制]
【发布时间】:2017-06-07 05:08:29
【问题描述】:

我正在尝试从像这样的 JSON 请求响应中接收数据:

{ “cnt”:1, “列表”:[{ “坐标”:{ “隆”:18.55, “纬度”:50.11 }, “系统”:{ “类型”:1, “身份证”:5356, “消息”:0.0095, "国家":"PL", “日出”:1496630290, “日落”:1496688673 }, “天气”:[{ “身份证”:800, “主要”:“清除”, "description":"晴朗的天空", “图标”:“01d” }], “主要的”:{ “温度”:293.71, “压力”:1014, “湿度”:42, “temp_min”:293.15, “最高温度”:294.15 }, “能见度”:10000, “风”:{ “速度”:4.1, “度数”:60 }, “云”:{ “全部”:0 }, “DT”:1496686603, “身份证”: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; }
}

请您解释一下,它应该如何工作以及我的错在哪里?

【问题讨论】:

  • 在 SO 上有无数个重复的问题,更不用说 json.net 上的一个可爱的帮助页面了。无需自定义方法。
  • 是的,你是对的。在询问之前,我试图先找到解决方案......但我没有足够的尝试。抱歉复制。下次我会更有耐心。

标签: c# json json.net


【解决方案1】:

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

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 字符串派生了上述类。

【讨论】:

  • @Downvoter 老实说,我会非常感谢您投反对票的理由。提前致谢
  • 我正在考虑这个问题。但我不需要所有这些数据。我完全需要 Main 和 Name 属性。无论如何声明所有这些类是否有意义?
  • 所以jsonutils.com 会自动对属性进行pascal-case 并选择性地添加[JsonProperty] 属性?谢谢 - 我以前没看过那个网站。
  • @JakubNowacki 我认为这种方式是有道理的,因为您定义了 json 的整个结构,以后如果您需要使用任何其他属性,则无需对代码进行任何更改: ) 你只会读它!
  • @dbc 是的,这是一个非常酷的工具,我在过去几周通过一位同事了解到它:) 不客气
猜你喜欢
  • 1970-01-01
  • 2017-09-20
  • 1970-01-01
  • 2020-03-29
  • 2017-03-10
  • 2019-08-03
  • 1970-01-01
  • 2015-10-02
  • 1970-01-01
相关资源
最近更新 更多