-2

I'm trying to deserialize JSON with C# and Newtonsoft.json. The raw json string looks like

{
"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]
    ]
}

note: the actual string has more of the "3c403f","FME ","Germany",1603710392,1603710392,13.499,52.366,null,true,9.77,337.5,null,null,null,null,false,0] parts, but i removed most, as it's ridiculously long.

Every time i go to deserialize it, i get the error:

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.

I am unable to modify the JSON layout, as its data fetched from the OpenSky API. The classes i use are:

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; }
    }
}

but i feel that is where it's going wrong.

RecievedData rd = JsonConvert.DeserializeObject<RecievedData>(downloadedContent); is being used to convert, and downloadedContent is the raw JSON

Any Advice?

  • 1
    Your `states` is an array of arrays, not an array with a `List` inside. Change your JSON to match your class structure or change your class to work with an array of arrays. – quaabaam Oct 27 '20 at 16:21
  • Please examine your classes and compare them to the JSON. Do you see an "ac" member in the JSON? No? Then why does it appear in your classes? – Ian Kemp Oct 27 '20 at 16:22
  • RecievedData class, property states should be List ac { get; set; } – Nonik Oct 27 '20 at 16:22
  • 2
    This should answer your question [Cannot deserialize the JSON array (e.g. \[1,2,3\]) into type ' ' because type requires JSON object (e.g. {"name":"value"}) to deserialize correctly](https://stackoverflow.com/questions/22557559/cannot-deserialize-the-json-array-e-g-1-2-3-into-type-because-type-requ), if not there are many, many others here with a similar title. – Ňɏssa Pøngjǣrdenlarp Oct 27 '20 at 16:35

3 Answers3

0

your Aircraft class has properties which in Json objects would look like "PropertyName": "PropertyValue" e.g. "icao24" : "3c403f"

What you have here is not a List<Aircraft> but an List<object[]> where you have to extract the values by their position and cast the objects to their type.

0

Depending on wether the parser is able to translate the array in the json into a list of Aircraft object, this may or may not work. Also you might have to make some fields nullable:

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; }
    }
}
Fuzzy
  • 486
  • 3
  • 7
0

I had a brainwave not long after posting. i simply used

public object[] states { get; set; }

in the RecievedData Class. It seems to work fine now.