繁体   English   中英

API返回的部分JSON已由JSON.NET序列化,但并非全部

[英]Part of JSON returned by API is serialised by JSON.NET but not all

我想这更多是我的代码问题,而不是JSON.NET问题,但我不确定我要去哪里。

我下面有以下用于将在此链接中找到的json数据序列化的类: https ://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid =b6907d289e10d714a6e88b30761fae22

为了构造这个类,我使用了在google上找到的JSON到C#生成器。

下面也是我用来将JSON数据提取并序列化为WeatherData对象的方法。

private string GetJsonFromWeb(string resource, string city)
{
    var request = new RestRequest(resource, Method.GET);
    request.AddParameter("q", city);
    request.AddParameter("APPID", "af5e6fd579e0ddb303afc1774487c77b");

    var fullUrl = client.BuildUri(request);
    Console.WriteLine("Full URL: " + fullUrl.AbsoluteUri);

    var response = client.Execute(request);
    string json = response.Content;
    return json;
}

private WeatherData SerializeJsonToWeatherData(string json)
{
    WeatherData weatherData = JsonConvert.DeserializeObject<WeatherData>(json);
    return weatherData;
}

这些方法可以成功工作,因为稍后可以在应用程序中使用JSON数据在GUI中显示信息。 但是,从API提取JSON后,JSON数据的MainData部分将设置为null。

class WeatherData
{
    public Coord Coord { get; set; }
    public List<Weather> Weather { get; set; }
    public string Base { get; set; }
    public MainData MainData { get; set; }
    public int Visibility { get; set; }
    public Wind Wind { get; set; }
    public Clouds Clouds { get; set; }
    public int Dt { get; set; }
    public Sys Sys { get; set; }
    public int Id { get; set; }
    public string Name { get; set; }
    public int Cod { get; set; }
}

public class Coord
{
    public double Lon { get; set; }
    public double Lat { 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 MainData
{
    public double Temp { get; set; }
    public int Pressure { get; set; }
    public int Humidity { get; set; }
    public double Temp_min { get; set; }
    public double Temp_max { get; set; }
}

public class Wind
{
    public double Speed { get; set; }
    public int Deg { get; set; }
}

public class Clouds
{
    public int All { get; set; }
}

public class Sys
{
    public int Type { get; set; }
    public int Id { get; set; }
    public double Message { get; set; }
    public string Country { get; set; }
    public int Sunrise { get; set; }
    public int Sunset { get; set; }
}

来自上述API的样本JSON

{
  "coord": {
    "lon": -0.13,
    "lat": 51.51
  },
  "weather": [
    {
      "id": 300,
      "main": "Drizzle",
      "description": "light intensity drizzle",
      "icon": "09d"
    }
  ],
  "base": "stations",
  "main": {
    "temp": 280.32,
    "pressure": 1012,
    "humidity": 81,
    "temp_min": 279.15,
    "temp_max": 281.15
  },
  "visibility": 10000,
  "wind": {
    "speed": 4.1,
    "deg": 80
  },
  "clouds": {
    "all": 90
  },
  "dt": 1485789600,
  "sys": {
    "type": 1,
    "id": 5091,
    "message": 0.0103,
    "country": "GB",
    "sunrise": 1485762037,
    "sunset": 1485794875
  },
  "id": 2643743,
  "name": "London",
  "cod": 200
}

JSON属性称为main而不是mainData JSON.Net找不到名为mainData ans的属性,因此不会填充MainData属性

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM