【发布时间】:2020-10-27 16:15:50
【问题描述】:
我正在尝试使用 C# 和 Newtonsoft.json 反序列化 JSON。原始 json 字符串看起来像
{
"time":1603710439,
"states":
[
["3c403f","FME ","Germany",1603710392,1603710392,13.499,52.366,null,true,9.77,337.5,null,null,null,null,false,0],
["ab6fdd","AAL377 ","United States",1603710241,1603710262,-79.2986,25.4875,6682.74,false,209.87,119.52,7.8,null,7063.74,null,false,0],
["880451","AIQ4307 ","Thailand",1603710208,1603710208,100.8166,13.5791,2849.88,false,151.87,15.12,-5.85,null,3017.52,null,false,0],
["39d228","FHURI ","France",1603710387,1603710387,6.2688,43.2912,1828.8,false,75.32,288.31,0,null,1851.66,null,false,0],
["a3687e","AJT8740 ","United States",1603710341,1603710341,-80.3101,25.8025,60.96,false,76.71,87.69,-3.9,null,38.1,null,false,0],
["7c6b1c","JST677 ","Australia",1603710222,1603710222,144.835,-37.6538,45.72,false,64.46,171.74,-2.93,null,160.02,"1051",false,0],
["a4ab49","N40LG ","United States",1603710439,1603710439,-81.0128,28.2367,4625.34,false,178.64,123.96,11.7,null,4876.8,null,false,0],
["a9b053","BTQ863 ","United States",1603710439,1603710439,-72.7255,43.9093,7604.76,false,143.34,129.17,0.33,null,7772.4,null,false,0]
]
}
注意:实际的字符串有更多 "3c403f","FME ","Germany",1603710392,1603710392,13.499,52.366,null,true,9.77,337.5,null,null,null,null,false,0] 部分,但我删除了大部分,因为它太长了。
每次我去反序列化它时,我都会收到错误:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'testForApiData.States' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path 'states[0]', line 1, position 30.
我无法修改 JSON 布局,因为它的数据是从 OpenSky API 获取的。我使用的类是:
namespace testForApiData
{
class RecievedData
{
public int time {get; set;}
public States[] states { get; set; }
}
class States
{
public List<Aircraft> ac { get; set; }
}
class Aircraft
{
public string icao24 { get; set; }
public string callsign { get; set; }
public string origin_country { get; set; }
public int time_position { get; set; }
public int last_contact { get; set; }
public float longitude { get; set; }
public float latitude { get; set; }
public float baro_altitude { get; set; }
public bool on_grounf { get; set; }
public float velocity { get; set; }
public float true_track { get; set; }
public float vertical_rate { get; set; }
public int[] sensors { get; set; }
public float geo_altitude { get; set; }
public string squark { get; set; }
public bool spi { get; set; }
public int position_source { get; set; }
}
}
但我觉得这就是问题所在。
RecievedData rd = JsonConvert.DeserializeObject<RecievedData>(downloadedContent);
正在用于转换,并且下载的内容是原始 JSON
有什么建议吗?
【问题讨论】:
-
您的
states是一个数组数组,而不是一个包含List<Aircraft>的数组。更改您的 JSON 以匹配您的类结构或更改您的类以使用数组数组。 -
请检查您的类并将它们与 JSON 进行比较。您在 JSON 中看到“ac”成员吗?不?那为什么它会出现在你的课程中呢?
-
RecievedData 类,属性状态应为 List
ac { get;放; }