使用拆分为两个类的属性对Json进行反序列化



我正试图从一个关于太阳辐射数据的api请求中反序列化一个JSON。json请求是以片段方式发出的。正如你所看到的;输入";类,具有Location属性,但是;Meta";类也有一个";输入";类和一个"类";位置";属性,就像信息被分为两个部分。其他3个类也会发生这种情况。

我对Json2csharp或//app.quicktype.io等自动类创建进行了一些测试。第一个页面只创建了一个名为Input的合并属性的类,但另一个页面创建了两个不同的类,就像Json所做的那样(Input和input1(。

第一个方法不起作用,第二个我没有尝试,因为我想被嘘,这是正确的方法。

如何思考是创造";型号";为取消认识这个Json而上课?


这是从HTTP API请求下载的Json,您可以看到有一个数组

{
"inputs":{
"location":{
"latitude":46.02,
"longitude":22.999,
"elevation":835.0
},
"meteo_data":{
"radiation_db":"PVGIS-SARAH",
"meteo_db":"ERA-Interim",
"year_min":2010,
"year_max":2011,
"use_horizon":true,
"horizon_db":null,
"horizon_data":"DEM-calculated"
},
"mounting_system":{
"fixed":{
"slope":{
"value":0,
"optimal":false
},
"azimuth":{
"value":0,
"optimal":false
},
"type":"free-standing"
}
},
"pv_module":{
"technology":null,
"peak_power":null,
"system_loss":null
}
},
"outputs":{
"hourly":[
{
"time":"20100101:0010",
"Gb(i)":0.0,
"Gd(i)":0.0,
"Gr(i)":0.0,
"H_sun":0.0,
"T2m":6.44,
"WS10m":1.64,
"Int":0.0
},
{
"time":"20100101:0110",
"Gb(i)":0.0,
"Gd(i)":0.0,
"Gr(i)":0.0,
"H_sun":0.0,
"T2m":6.55,
"WS10m":1.52,
"Int":0.0
},
{
"time":"20100101:0210",
"Gb(i)":0.0,
"Gd(i)":0.0,
"Gr(i)":0.0,
"H_sun":0.0,
"T2m":6.66,
"WS10m":1.39,
"Int":0.0
}
]
},
"meta":{
"inputs":{
"location":{
"description":"Selected location",
"variables":{
"latitude":{
"description":"Latitude",
"units":"decimal degree"
},
"longitude":{
"description":"Longitude",
"units":"decimal degree"
},
"elevation":{
"description":"Elevation",
"units":"m"
}
}
},
"meteo_data":{
"description":"Sources of meteorological data",
"variables":{
"radiation_db":{
"description":"Solar radiation database"
},
"meteo_db":{
"description":"Database used for meteorological variables other than solar radiation"
},
"year_min":{
"description":"First year of the calculations"
},
"year_max":{
"description":"Last year of the calculations"
},
"use_horizon":{
"description":"Include horizon shadows"
},
"horizon_db":{
"description":"Source of horizon data"
}
}
},
"mounting_system":{
"description":"Mounting system",
"choices":"fixed, vertical_axis, inclined_axis, two_axis",
"fields":{
"slope":{
"description":"Inclination angle from the horizontal plane",
"units":"degree"
},
"azimuth":{
"description":"Orientation (azimuth) angle of the (fixed) PV system (0 = S, 90 = W, -90 = E)",
"units":"degree"
}
}
},
"pv_module":{
"description":"PV module parameters",
"variables":{
"technology":{
"description":"PV technology"
},
"peak_power":{
"description":"Nominal (peak) power of the PV module",
"units":"kW"
},
"system_loss":{
"description":"Sum of system losses",
"units":"%"
}
}
}
},
"outputs":{
"hourly":{
"type":"time series",
"timestamp":"hourly averages",
"variables":{
"Gb(i)":{
"description":"Beam (direct) irradiance on the inclined plane (plane of the array)",
"units":"W/m2"
},
"Gd(i)":{
"description":"Diffuse irradiance on the inclined plane (plane of the array)",
"units":"W/m2"
},
"Gr(i)":{
"description":"Reflected irradiance on the inclined plane (plane of the array)",
"units":"W/m2"
},
"H_sun":{
"description":"Sun height",
"units":"degree"
},
"T2m":{
"description":"2-m air temperature",
"units":"degree Celsius"
},
"WS10m":{
"description":"10-m total wind speed",
"units":"m/s"
},
"Int":{
"description":"1 means solar radiation values are reconstructed"
}
}
}
}
}
}

以下是从Json2csharp网站自动创建的未实现类:

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);

public class Location
{
public double latitude { get; set; }
public double longitude { get; set; }
public double elevation { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class MeteoData
{
public string radiation_db { get; set; }
public string meteo_db { get; set; }
public int year_min { get; set; }
public int year_max { get; set; }
public bool use_horizon { get; set; }
public object horizon_db { get; set; }
public string horizon_data { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class Slope
{
public int value { get; set; }
public bool optimal { get; set; }
public string description { get; set; }
public string units { get; set; }
}
public class Azimuth
{
public int value { get; set; }
public bool optimal { get; set; }
public string description { get; set; }
public string units { get; set; }
}
public class Fixed
{
public Slope slope { get; set; }
public Azimuth azimuth { get; set; }
public string type { get; set; }
}
public class MountingSystem
{
public Fixed @fixed { get; set; }
public string description { get; set; }
public string choices { get; set; }
public Fields fields { get; set; }
}
public class PvModule
{
public object technology { get; set; }
public object peak_power { get; set; }
public object system_loss { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class Inputs
{
public Location location { get; set; }
public MeteoData meteo_data { get; set; }
public MountingSystem mounting_system { get; set; }
public PvModule pv_module { get; set; }
}
public class Hourly
{
public string time { get; set; }
[JsonProperty("Gb(i)")]
public double GbI { get; set; }
[JsonProperty("Gd(i)")]
public double GdI { get; set; }
[JsonProperty("Gr(i)")]
public double GrI { get; set; }
public double H_sun { get; set; }
public double T2m { get; set; }
public double WS10m { get; set; }
public double Int { get; set; }
public string type { get; set; }
public string timestamp { get; set; }
public Variables variables { get; set; }
}
public class Outputs
{
public List<Hourly> hourly { get; set; }
}
public class Latitude
{
public string description { get; set; }
public string units { get; set; }
}
public class Longitude
{
public string description { get; set; }
public string units { get; set; }
}
public class Elevation
{
public string description { get; set; }
public string units { get; set; }
}
public class Variables
{
public Latitude latitude { get; set; }
public Longitude longitude { get; set; }
public Elevation elevation { get; set; }
public RadiationDb radiation_db { get; set; }
public MeteoDb meteo_db { get; set; }
public YearMin year_min { get; set; }
public YearMax year_max { get; set; }
public UseHorizon use_horizon { get; set; }
public HorizonDb horizon_db { get; set; }
public Technology technology { get; set; }
public PeakPower peak_power { get; set; }
public SystemLoss system_loss { get; set; }
[JsonProperty("Gb(i)")]
public GbI GbI { get; set; }
[JsonProperty("Gd(i)")]
public GdI GdI { get; set; }
[JsonProperty("Gr(i)")]
public GrI GrI { get; set; }
public HSun H_sun { get; set; }
public T2m T2m { get; set; }
public WS10m WS10m { get; set; }
public Int Int { get; set; }
}
public class RadiationDb
{
public string description { get; set; }
}
public class MeteoDb
{
public string description { get; set; }
}
public class YearMin
{
public string description { get; set; }
}
public class YearMax
{
public string description { get; set; }
}
public class UseHorizon
{
public string description { get; set; }
}
public class HorizonDb
{
public string description { get; set; }
}
public class Fields
{
public Slope slope { get; set; }
public Azimuth azimuth { get; set; }
}
public class Technology
{
public string description { get; set; }
}
public class PeakPower
{
public string description { get; set; }
public string units { get; set; }
}
public class SystemLoss
{
public string description { get; set; }
public string units { get; set; }
}
public class GbI
{
public string description { get; set; }
public string units { get; set; }
}
public class GdI
{
public string description { get; set; }
public string units { get; set; }
}
public class GrI
{
public string description { get; set; }
public string units { get; set; }
}
public class HSun
{
public string description { get; set; }
public string units { get; set; }
}
public class T2m
{
public string description { get; set; }
public string units { get; set; }
}
public class WS10m
{
public string description { get; set; }
public string units { get; set; }
}
public class Int
{
public string description { get; set; }
}
public class Meta
{
public Inputs inputs { get; set; }
public Outputs outputs { get; set; }
}
public class Root
{
public Inputs inputs { get; set; }
public Outputs outputs { get; set; }
public Meta meta { get; set; }
}

我已经尝试了这两种方法来反序列化这个单一对象

尝试#1:

public Root DeserialiceJson(string jsonResponse)
{
Root deserializedJsonClass = JsonConvert.DeserializeObject<Root>(jsonResponse);
return deserializedJsonClass;
}

这次尝试#2:

public List<Root> DeserialiceJsonArray(string jsonResponse)
{
List<Root> deserializedJsonClass = JsonConvert.DeserializeObject<List<Root>>(jsonResponse);
return deserializedJsonClass;
}

它们都不起作用。

我走对了吗?

在JSON对象中;输出";在";元";与根对象中其他输出中的不同。

您在一个位置得到一个列表,而在第二个位置得到单个对象。您应该定义一个合适的格式来接收这个JSON。

现在您可以使用这些类。

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse); 
public class Location
{
public double latitude { get; set; }
public double longitude { get; set; }
public double elevation { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class MeteoData
{
public string radiation_db { get; set; }
public string meteo_db { get; set; }
public int year_min { get; set; }
public int year_max { get; set; }
public bool use_horizon { get; set; }
public object horizon_db { get; set; }
public string horizon_data { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class Slope
{
public int value { get; set; }
public bool optimal { get; set; }
public string description { get; set; }
public string units { get; set; }
}
public class Azimuth
{
public int value { get; set; }
public bool optimal { get; set; }
public string description { get; set; }
public string units { get; set; }
}
public class Fixed
{
public Slope slope { get; set; }
public Azimuth azimuth { get; set; }
public string type { get; set; }
}
public class MountingSystem
{
public Fixed @fixed { get; set; }
public string description { get; set; }
public string choices { get; set; }
public Fields fields { get; set; }
}
public class PvModule
{
public object technology { get; set; }
public object peak_power { get; set; }
public object system_loss { get; set; }
public string description { get; set; }
public Variables variables { get; set; }
}
public class Inputs
{
public Location location { get; set; }
public MeteoData meteo_data { get; set; }
public MountingSystem mounting_system { get; set; }
public PvModule pv_module { get; set; }
}
public class Hourly
{
public string time { get; set; }
[JsonProperty("Gb(i)")]
public string GbI { get; set; }
[JsonProperty("Gd(i)")]
public double GdI { get; set; }
[JsonProperty("Gr(i)")]
public string GrI { get; set; }
public string H_sun { get; set; }
public string T2m { get; set; }
public string WS10m { get; set; }
public string Int { get; set; }
}
public class Outputs
{
public List<Hourly> hourly { get; set; }
}
public class Latitude
{
public string description { get; set; }
public string units { get; set; }
}
public class Longitude
{
public string description { get; set; }
public string units { get; set; }
}
public class Elevation
{
public string description { get; set; }
public string units { get; set; }
}
public class Variables
{
public Latitude latitude { get; set; }
public Longitude longitude { get; set; }
public Elevation elevation { get; set; }
public RadiationDb radiation_db { get; set; }
public MeteoDb meteo_db { get; set; }
public YearMin year_min { get; set; }
public YearMax year_max { get; set; }
public UseHorizon use_horizon { get; set; }
public HorizonDb horizon_db { get; set; }
public Technology technology { get; set; }
public PeakPower peak_power { get; set; }
public SystemLoss system_loss { get; set; }
[JsonProperty("Gb(i)")]
public GbI GbI { get; set; }
[JsonProperty("Gd(i)")]
public GdI GdI { get; set; }
[JsonProperty("Gr(i)")]
public GrI GrI { get; set; }
public HSun H_sun { get; set; }
public T2m T2m { get; set; }
public WS10m WS10m { get; set; }
public Int Int { get; set; }
}
public class RadiationDb
{
public string description { get; set; }
}
public class MeteoDb
{
public string description { get; set; }
}
public class YearMin
{
public string description { get; set; }
}
public class YearMax
{
public string description { get; set; }
}
public class UseHorizon
{
public string description { get; set; }
}
public class HorizonDb
{
public string description { get; set; }
}
public class Fields
{
public Slope slope { get; set; }
public Azimuth azimuth { get; set; }
}
public class Technology
{
public string description { get; set; }
}
public class PeakPower
{
public string description { get; set; }
public string units { get; set; }
}
public class SystemLoss
{
public string description { get; set; }
public string units { get; set; }
}
public class GbI
{
public string description { get; set; }
public string units { get; set; }
}
public class GdI
{
public string description { get; set; }
public string units { get; set; }
}
public class GrI
{
public string description { get; set; }
public string units { get; set; }
}
public class HSun
{
public string description { get; set; }
public string units { get; set; }
}
public class T2m
{
public string description { get; set; }
public string units { get; set; }
}
public class WS10m
{
public string description { get; set; }
public string units { get; set; }
}
public class Int
{
public string description { get; set; }
}
public class Meta
{
public Inputs inputs { get; set; }
public Outputs2 outputs { get; set; }
}
public class Hourly2
{
public string type { get; set; }
public string timestamp { get; set; }
public Variables variables { get; set; }
}
public class Outputs2
{
public Hourly2 hourly { get; set; }
}
public class Root
{
public Inputs inputs { get; set; }
public Outputs outputs { get; set; }
public Meta meta { get; set; }
}

并像这个一样反序列化

var root = JsonConvert.DeserializeObject<Root>(json);

最新更新