【发布时间】:2017-12-08 15:52:18
【问题描述】:
原始的 JSON C# 类结构看起来像
public class Main
{
public double temp { get; set; }
public double temp_min { get; set; }
public double temp_max { get; set; }
public double pressure { get; set; }
public double sea_level { get; set; }
public double grnd_level { get; set; }
public int humidity { get; set; }
public double temp_kf { 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 Clouds
{
public int all { get; set; }
}
public class Wind
{
public double speed { get; set; }
public double deg { get; set; }
}
public class Snow
{
public double? 3h { get; set; }
}
public class Sys
{
public string pod { get; set; }
}
public class Rain
{
public double 3h { get; set; }
}
public class List
{
public int dt { get; set; }
public Main main { get; set; }
public IList<Weather> weather { get; set; }
public Clouds clouds { get; set; }
public Wind wind { get; set; }
public Snow snow { get; set; }
public Sys sys { get; set; }
public string dt_txt { get; set; }
public Rain rain { get; set; }
}
public class Coord
{
public double lat { get; set; }
public double lon { 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 class Example
{
public string cod { get; set; }
public double message { get; set; }
public int cnt { get; set; }
public IList<List> list { get; set; }
public City city { get; set; }
}
我想删除一些属性并将其他属性合并到我的类型中,所以最后我想获得下一个 C# 类结构:
public class WeatherForecast
{
[JsonProperty("city")]
public City City { get; set; }
[JsonProperty("list")]
public IList<ClimateIndicators> ClimateIndicators { get; set; }
}
public class ClimateIndicators
{
public Atmospheric Atmospheric { get; set; }
public Hydrospheric Hydrospheric { get; set; }
public Lithospheric Lithospheric { get; set; }
public DateTime Date { get; set; }
public IList<WeatherDetails> WeatherDetails { get; set; }
}
public class Atmospheric
{
[JsonProperty("pressure")]
public double Pressure { get; set; }
[JsonProperty("humidity")]
public int Humidity { get; set; }
[JsonProperty("sea_level")]
public double SeaLevel { get; set; }
[JsonProperty("grnd_level")]
public double GroundLevel { get; set; }
[JsonProperty("all")]
public int Cloudiness { get; set; }
[JsonProperty("rain")]
public double? Rain { get; set; }
[JsonProperty("snow")]
public double? Snow { get; set; }
}
我正在为天气状况添加自定义分类。所以我面临的问题是我不知道如何将多个 JSON 属性合并到一个类中,该类是另一个 JSON 属性的属性。
【问题讨论】:
-
你的 json 在哪里?
-
您要反序列化的 JSON 是什么,它与您要实现的新模型之间的映射是什么?此外,您想要的模型缺少一堆类。
Hydrospheric、Lithospheric和WeatherDetails的定义是什么? -
@BrianRogers samples.openweathermap.org/data/2.5/… 忽略这三个。我想将 Clouds Wind Snow 和 Main 结合到 Atmospheric 类中。如何在反序列化时创建 Atmospheric 对象并将这些属性移入其中?
标签: c# json json.net json-deserialization