【发布时间】:2021-07-15 00:28:49
【问题描述】:
我正在尝试从 api 请求中反序列化一个关于太阳辐射数据的 JSON。 json请求以片段方式进行。如您所见,“inputs”类有一个 Location 属性,但“Meta”类也有一个“inputs”类和一个“location”属性,就像信息被分成 2 个部分一样。这也发生在其他 3 个类中。
我已经使用 Json2csharp 或 //app.quicktype.io 等自动创建类进行了一些测试。第一个页面只创建 1 个合并属性的类 Input,但另一个页面创建 2 个不同的类,就像 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;
}
它们都不起作用。
我走对了吗?
【问题讨论】:
-
None works是什么意思?您面临的问题是什么? -
恕我直言,使用此 json 工具的输出作为指导,然后手动编辑类定义以创建您实际需要的类型。
标签: c# json deserialization