恐怕這可能會很長。我有一份記錄清單。每條記錄都有記錄詳細資訊。我得到了看起來像這樣的 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"
},
我有一個記錄類:
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.
任何幫助將不勝感激!
uj5u.com熱心網友回復:
你必須創建整個類,而不僅僅是一個陣列部分。試試這個
Data data=JsonConvert.DeserializeObject<Data>(jsonString )
或者如果您只需要alarmRecords,則不需要D 和PagedAlarms 類。試試這個代碼
var jsonParsed=JObject.Parse(json);
List<AlarmRecord> alarmRecords=jsonParsed["d"]["paged_alarms"]["alarm_records"].ToObject<List<AlarmRecord>>();
班級
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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/463025.html