我有一个返回附带JSON响应的API调用,但在反序列化时出现以下错误:"Error converting value 0 to type '....Models.TripMaps.Alarms'. Path 'map[2].Alarm', line 1, position 1354."
所以第二个地图数组项的警报值为0(而不是预期的Alarm对象)。
我该如何处理这种情况?非常感谢任何帮助。
{
"metrics":"1",
"map":[
{
"GPRSPoints":{
"ID":"349709508",
"DataReceived":"2019-12-16 11:30:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":0,
"Latitude":-37.81026166666667,
"Liters":0,
"Longitude":144.962755,
"RPM":-1,
"Speed":0
},
"Alarm":{
"ID":"349709508",
"Alarm":"Ignition On",
"AlarmId":"16",
"AlarmAddress":"Melbourne Central, Elizabeth Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
"AlarmThreshold":"0",
"DataReceived":"2019-12-16 11:30:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Latitude":-37.81026166666667,
"Longitude":144.962755,
"Liters":0,
"RPM":-1,
"Speed":0
}
},
{
"GPRSPoints":{
"ID":"349709519",
"DataReceived":"2019-12-16 11:30:27",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":0,
"Latitude":-37.81026166666667,
"Liters":0,
"Longitude":144.962755,
"RPM":-1,
"Speed":0
},
"Alarm":{
"ID":"349709519",
"Alarm":"Fatigue driving",
"AlarmId":"0D",
"AlarmAddress":"Melbourne Central, Elizabeth Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
"AlarmThreshold":"240",
"DataReceived":"2019-12-16 11:30:27",
"IgnitionOn":"2019-12-16 11:30:07",
"Latitude":-37.81026166666667,
"Longitude":144.962755,
"Liters":0,
"RPM":-1,
"Speed":0
}
},
{
"GPRSPoints":{
"ID":"349709550",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":0,
"Latitude":-37.81026166666667,
"Liters":0,
"Longitude":144.962755,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709551",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":0,
"Latitude":-37.81026166666667,
"Liters":0,
"Longitude":144.962755,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709552",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":0,
"Latitude":-37.81026166666667,
"Liters":0,
"Longitude":144.962755,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709554",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":163.9,
"Latitude":-37.81124166666667,
"Liters":0,
"Longitude":144.96304833333335,
"RPM":0,
"Speed":1.0799999999999998
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709555",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":153.8,
"Latitude":-37.80994333333334,
"Liters":0,
"Longitude":144.962625,
"RPM":0,
"Speed":0.612
},
**"Alarm":0**
},
{
"GPRSPoints":{
"ID":"349709556",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":6.4,
"Latitude":-37.809958333333334,
"Liters":0,
"Longitude":144.96257666666668,
"RPM":0,
"Speed":1.5479999999999998
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709557",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":343.9,
"Latitude":-37.80993,
"Liters":0,
"Longitude":144.96251333333333,
"RPM":0,
"Speed":1.188
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709558",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":343.9,
"Latitude":-37.80992666666667,
"Liters":0,
"Longitude":144.96246666666667,
"RPM":0,
"Speed":0.39599999999999996
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709559",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":343.9,
"Latitude":-37.809938333333335,
"Liters":0,
"Longitude":144.96243333333334,
"RPM":0,
"Speed":0.36
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709560",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":343.9,
"Latitude":-37.80994666666667,
"Liters":0,
"Longitude":144.962395,
"RPM":0,
"Speed":0.7559999999999999
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709561",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":156.1,
"Latitude":-37.80976666666667,
"Liters":0,
"Longitude":144.96213,
"RPM":0,
"Speed":1.404
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709562",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809868333333334,
"Liters":0,
"Longitude":144.96237,
"RPM":0,
"Speed":0.5399999999999999
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709563",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809875,
"Liters":0,
"Longitude":144.96233833333332,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709564",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709565",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709566",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709567",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709568",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709569",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709570",
"DataReceived":"2019-12-16 11:31:20",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710609",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710610",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710612",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710613",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710614",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710615",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710616",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349710617",
"DataReceived":"2019-12-16 11:31:44",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709573",
"DataReceived":"2019-12-16 11:31:49",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":0,
"Speed":0
},
"Alarm":0
},
{
"GPRSPoints":{
"ID":"349709574",
"DataReceived":"2019-12-16 11:31:51",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":-1,
"Speed":0
},
"Alarm":{
"ID":"349709574",
"Alarm":"Ignition Off",
"AlarmId":"17",
"AlarmAddress":"Aurora Melbourne Central, Little La Trobe Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
"AlarmThreshold":"0",
"DataReceived":"2019-12-16 11:31:51",
"IgnitionOn":"2019-12-16 11:30:07",
"Latitude":-37.809873333333336,
"Longitude":144.96232666666666,
"Liters":0,
"RPM":-1,
"Speed":0
}
},
{
"GPRSPoints":{
"ID":"349709576",
"DataReceived":"2019-12-16 11:31:51",
"IgnitionOn":"2019-12-16 11:30:07",
"Direction":291.4,
"Latitude":-37.809873333333336,
"Liters":0,
"Longitude":144.96232666666666,
"RPM":-1,
"Speed":0
},
"Alarm":{
"ID":"349709576",
"Alarm":"Fatigue driving",
"AlarmId":"0D",
"AlarmAddress":"Aurora Melbourne Central, Little La Trobe Street, Melbourne City, City of Melbourne, Victoria, 3000, Australia",
"AlarmThreshold":"240",
"DataReceived":"2019-12-16 11:31:51",
"IgnitionOn":"2019-12-16 11:30:07",
"Latitude":-37.809873333333336,
"Longitude":144.96232666666666,
"Liters":0,
"RPM":-1,
"Speed":0
}
}
]
}
这些是我的模型类
public class MapResponse
{
[JsonProperty("metrics")]
public long metrics { get; set; }
[JsonProperty("map")]
public Map[] map { get; set; }
}
并且
public class Map
{
[JsonProperty("GPRSPoints")]
public GprsPoints GprsPoints { get; set; }
[JsonProperty("Alarm")]
public Alarms Alarm { get; set; }
}
我将如下进行反序列化:
{code}
MapResponse result = JsonConvert.DeserializeObject<MapResponse>(res);
我想当JSON值为0时,将警报设置为空。
JsonConverter
来处理此问题。当 JSON 中有"Alarm":0
时,您希望为Alarm
设置什么值?此外,您能否确认正在使用 [tag:json.net],例如通过显示异常的完整ToString()
输出,包括异常类型、消息、回溯和内部异常(如果有)? - dbc