【问题标题】:Deserialize json object that is sometimes an int value反序列化有时是 int 值的 json 对象
【发布时间】:2019-12-18 03:06:16
【问题描述】:

我有一个返回附加 JSON 响应的 API 调用,在反序列化时我收到以下错误 “错误将值 0 转换为类型 '....Models.TripMaps.Alarms'。路径 'map[2] .Alarm',第 1 行,位置 1354。”

因此,第二个地图数组项的警报值为 0(而不是预期的警报对象)。

我该如何处理?任何帮助表示赞赏

{
   "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; }
}

我反序列化如下:

MapResponse result = JsonConvert.DeserializeObject<MapResponse>(res); 

我想当 JSON 值为 0 时,我希望 Alarm 为空。

【问题讨论】:

  • 错误消息似乎来自json.net 序列化程序。您可以使用自定义JsonConverter 处理此问题。当 JSON 中有 "Alarm":0 时,您希望 Alarm 的值是多少?另外,您能否确认正在使用json.net,例如通过显示异常的完整ToString() 输出,包括异常类型、消息、回溯和内部异常(如果有)?
  • MapResponse 结果 = JsonConvert.DeserializeObject(res);我想我希望 Alarm 在 0 时为空
  • 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, CultureInfoculture, JsonContract contract, Type targetType)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInterna...

标签: c# json .net-core json.net json-deserialization


【解决方案1】:

如果"Alarm" 的值是整数,您可以引入custom JsonConverter,它返回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

演示小提琴here.

【讨论】:

    猜你喜欢
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-06
    相关资源
    最近更新 更多