反序列化列表列表

Deserialize a list of lists

恐怕这会很长。我有一个记录列表。每条记录都有记录详细信息。我回来 json 看起来像这样

d": {
        "__type": "xVehicleAlarm.xVehicleAlarmResult:#AgDNA.AppWebServices.AccessLayer",
        "account_id": "4ef----ef49-414b-98cb-08ab5b1c354f",
        "message": null,
        "paged_alarms": {
            "__type": "VehicleAlarmsPagedResult:#AgDNA.AppWebServices.AccessLayer",
            "alarm_records": [
                {
                    "__type": "AlarmRecord:#AgDNA.AppWebServices.AccessLayer",
                    "alarm_record_details": [
                        {
                            "__type": "AlarmRecordDetail:#AgDNA.AppWebServices.AccessLayer",
                            "active": false,
                            "code": "AG-170",
                            "color": "#7F7F7F",
                             "title": "Cannot Engage Automatic"
                        }
                    ],
                    "lat": 1.58868408203125,
                    "lon": 1.47056454420089722,
                    "time": "4/6/2022 10:05:08 AM"
                },
                {
                    "__type": "AlarmRecord:#AgDNA.AppWebServices.AccessLayer",
                    "alarm_record_details": [
                        {
                            "__type": "AlarmRecordDetail:#AgDNA.AppWebServices.AccessLayer",
                            "active": true,
                            "code": "AG-170",
                            "color": "#FFFF00",
                            "title": "Cannot Engage Automatic"
                        }
                    ],
                    "lat": 1.588680267333984,
                    "lon": 1.47049078345298767,
                    "time": "4/6/2022 10:05:06 AM"
                },

我有一条记录class:

 public class VehicleAlarmRecordBase
 {
     [JsonProperty("lat")]
     public virtual double Latitude
     {
         get;
         set;
     }
     [JsonProperty("lon")]
     public virtual double Longitude
     {
         get;
         set;
     }

     [JsonProperty("alarm_record_details")]
     public virtual VehicleAlarmRecordDetail[] Records
     {
         get;
         set;
     }

     [JsonProperty("time")]
     public virtual string Time { get; set; }

注意定义为(摘录)的 vehicleAlarmRecordDetail 列表

      [JsonProperty("account_id")]
  public virtual Guid AccountId
  {
      get;
      set;
  }
  [JsonProperty("vehicle_id")]
  public virtual Guid VehicleId
  {
      get;
      set;
  }

  [JsonProperty("active")]
  public virtual string Active
  {
      get;
      set;
  }

我调用反序列化:

    var jsonString = response.Item2.ToString();
var vehicleAlarms = JsonConvert.DeserializeObject<List<VehicleAlarmRecord>>(jsonString);

我收到错误:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[AgDNA.Services.Models.VehicleAlarmRecord]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.

如有任何帮助,非常感谢!!

您必须创建整个 class,而不仅仅是数组部分。试试这个

Data data=JsonConvert.DeserializeObject<Data>(jsonString )

或者如果您只需要 alarmRecords,则不需要 D 和 PagedAlarms classes。试试这个代码

var jsonParsed=JObject.Parse(json);

List<AlarmRecord> alarmRecords=jsonParsed["d"]["paged_alarms"]["alarm_records"].ToObject<List<AlarmRecord>>();

classes

    public partial class Data
    {
        [JsonProperty("d")]
        public D D { get; set; }
    }

    public partial class D
    {
        [JsonProperty("__type")]
        public string Type { get; set; }

        [JsonProperty("account_id")]
        public string AccountId { get; set; }

        [JsonProperty("message")]
        public object Message { get; set; }

        [JsonProperty("paged_alarms")]
        public PagedAlarms PagedAlarms { get; set; }
    }

    public partial class PagedAlarms
    {
        [JsonProperty("__type")]
        public string Type { get; set; }

        [JsonProperty("alarm_records")]
        public List<AlarmRecord> AlarmRecords { get; set; }
    }

    public partial class AlarmRecord
    {
        [JsonProperty("__type")]
        public string Type { get; set; }

        [JsonProperty("alarm_record_details")]
        public List<AlarmRecordDetail> AlarmRecordDetails { get; set; }

        [JsonProperty("lat")]
        public double Lat { get; set; }

        [JsonProperty("lon")]
        public double Lon { get; set; }

        [JsonProperty("time")]
        public string Time { get; set; }
    }

    public partial class AlarmRecordDetail
    {
        [JsonProperty("__type")]
        public string Type { get; set; }

        [JsonProperty("active")]
        public bool Active { get; set; }

        [JsonProperty("code")]
        public string Code { get; set; }

        [JsonProperty("color")]
        public string Color { get; set; }

        [JsonProperty("title")]
        public string Title { get; set; }
    }