I'm afraid this may be a long one. I have a list of records. Each record has record details. I get back json that looks like this
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"
},
I have a Record 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; }
Notice the list of vehicleAlarmRecordDetail which is defined as (excerpt)
[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;
}
I call the deserializatoin:
var jsonString = response.Item2.ToString();
var vehicleAlarms = JsonConvert.DeserializeObject<List<VehicleAlarmRecord>>(jsonString);
I get the error:
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.
Any help would be greatly appreciated!!
CodePudding user response:
you have to create the whole class, not just an array part. try this
Data data=JsonConvert.DeserializeObject<Data>(jsonString )
or if you need just alarmRecords, you don't need D and PagedAlarms classes. try this code
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; }
}