反序列化有时为整数值的JSON对象

3

我有一个返回附带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时,将警报设置为空。


错误信息似乎来自于 [tag:json.net] 序列化器。您可以使用自定义的 JsonConverter 来处理此问题。当 JSON 中有 "Alarm":0 时,您希望为 Alarm 设置什么值?此外,您能否确认正在使用 [tag:json.net],例如通过显示异常的完整 ToString() 输出,包括异常类型、消息、回溯和内部异常(如果有)? - dbc
MapResponse result = JsonConvert.DeserializeObject<MapResponse>(res); 我猜当为0时,我希望Alarm为空。 - Possam
ex = {"将值0转换为类型'HKDData.Models.TripMaps.Alarms'时出错。路径'map[2].Alarm',行1,位置1354。"} InnerException = {"无法将System.Int64转换为HKDData.Models.TripMaps.Alarms。"} StackTrace = "在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)中发生\r\n 在Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInterna... - Possam
1个回答

4
你可以引入一个自定义的 JsonConverter,当"Alarm"的值是整数时返回null
public class AlarmsConverter : JsonConverter<Alarms>
{
    public override Alarms ReadJson(JsonReader reader, Type objectType, Alarms existingValue, bool hasExistingValue, JsonSerializer serializer)
    {
        switch (reader.MoveToContentAndAssert().TokenType)
        {
            case JsonToken.Null:
            case JsonToken.Integer:
                return null;

            default:
                var alarm = hasExistingValue ? existingValue : (Alarms)serializer.ContractResolver.ResolveContract(objectType).DefaultCreator();
                serializer.Populate(reader, alarm);
                return alarm;
        }
    }

    public override bool CanWrite => false;

    public override void WriteJson(JsonWriter writer, Alarms value, JsonSerializer serializer) => throw new NotImplementedException();
}

public static partial class JsonExtensions
{
    public static JsonReader MoveToContentAndAssert(this JsonReader reader)
    {
        if (reader == null)
            throw new ArgumentNullException();
        if (reader.TokenType == JsonToken.None)       // Skip past beginning of stream.
            reader.ReadAndAssert();
        while (reader.TokenType == JsonToken.Comment) // Skip past comments.
            reader.ReadAndAssert();
        return reader;
    }

    public static JsonReader ReadAndAssert(this JsonReader reader)
    {
        if (reader == null)
            throw new ArgumentNullException();
        if (!reader.Read())
            throw new JsonReaderException("Unexpected end of JSON stream.");
        return reader;
    }
}

将其应用于 Alarm 属性,如下所示:
public class Map
{
    [JsonProperty("Alarm")]
    [JsonConverter(typeof(AlarmsConverter))]
    public Alarms Alarm { get; set; }

    // Remainder unchanged

演示 fiddle 这里


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接