【问题标题】:Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'testForApiData.States' because the type requires a JSON object [duplicate]无法将当前 JSON 数组(例如 [1,2,3])反序列化为“testForApiData.States”类型,因为该类型需要 JSON 对象 [重复]
【发布时间】: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&lt;RecievedData&gt;(downloadedContent); 正在用于转换,并且下载的内容是原始 JSON

有什么建议吗?

【问题讨论】:

标签: c# json .net json.net


【解决方案1】:

您的 Aircraft 类具有在 Json 对象中看起来像 "PropertyName": "PropertyValue" 的属性,例如"icao24" : "3c403f"

您在这里拥有的不是List&lt;Aircraft&gt;,而是List&lt;object[]&gt;,您必须在其中按位置提取值并将对象转换为它们的类型。

【讨论】:

    【解决方案2】:

    根据解析器是否能够将 json 中的数组转换为 Aircraft 对象列表,这可能有效,也可能无效。此外,您可能必须使某些字段可以为空:

    namespace testForApiData
    {
        class RecievedData
        {
            public int time {get; set;}
            public List<Aircraft> states { 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; }
        }
    }
    

    【讨论】:

      【解决方案3】:

      发帖后不久,我有了一个脑电波。我只是用过

      public object[] states { get; set; }
      

      在 RecievedData 类中。它现在似乎工作正常。

      【讨论】:

        猜你喜欢
        • 2022-01-07
        • 1970-01-01
        • 2020-07-03
        • 2022-06-11
        • 2019-04-30
        • 1970-01-01
        • 1970-01-01
        • 2019-03-18
        • 1970-01-01
        相关资源
        最近更新 更多