c# - 反序列化有时是 int 值的 json 对象

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

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

因此,第二个 map 数组项的警报值为 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 为 null。

最佳答案

你可以介绍一个custom 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 here .

关于c# - 反序列化有时是 int 值的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384918/

相关文章:

C# 数组索引越界错误

json - CF 在 cf2016 中接受 JSON 字符串,但在 cf9 中不接受

c# - 困惑 : ASP. NET Core 2 不是托管代码,但 ASP.Net Core Runtime 是做什么用的?

c# - 在 C# 中抛出异常,守卫

c# - 如何使用异常处理创建永无止境的 DataFlow Mesh?

c# - 图像拖放(WPF 窗口)

java json字符串到对象的数组列表

java - 过滤包含部分数组的 JSON 文件

linux - 如何将 Windows Identity 从 Angular 应用程序传递到托管在 Linux 服务器上的 asp.net core 2.0 webapi

c# - dynamic 关键字影响返回类型