I am getting response from the following public API, which contains flight information (contains 12 fields each). I am trying deserialize this response into my object structure using Newtonsoft.JSON. I don't really need to use this package - also recommendations are welcome.
The API response that contains the flight data (WARNING, it's kinda a big response)
The parsed response looks something like following:
{
"time": 1484343110,
"states": [
[
"a25f32",
"N252CV ",
"United States",
1484343109,
1484343109,
-86.9989,
40.5451,
2209.8,
false,
95.05,
327.6,
4.55,
null
],
[
"aac8a4",
"UAL35 ",
"United States",
1484343108,
1484343108,
-128.0702,
43.3022,
9144,
false,
220.81,
323.72,
0,
null
]]
}
First problem: The root element contains two values, which are "time" and "states" which is an array of flight information. Therefore, I tried to create my class structures like following:
/// <summary>
/// Field information is taken from
/// https://opensky-network.org/apidoc/rest.html
/// </summary>
public class FlightInformation
{
[JsonProperty("time")]
public string Timestamp { get; set; }
[JsonProperty("states")]
public List<FlightDetail> FlightDetails { get; set; }
}
public class FlightDetail
{
[JsonProperty(PropertyName = "icao24")]
public string ICAOAddress { get; set; }
[JsonProperty(PropertyName = "callsign")]
public string AircraftCallsign { get; set; }
[JsonProperty(PropertyName = "country")]
public string OriginCountry { get; set; }
[JsonProperty(PropertyName = "time_position")]
public string LastPositionUpdate { get; set; }
[JsonProperty(PropertyName = "time_velocity")]
public string LastVelocityUpdate { get; set; }
[JsonProperty(PropertyName = "longitude")]
public string Longitude { get; set; }
[JsonProperty(PropertyName = "latitude")]
public string Latitude { get; set; }
[JsonProperty(PropertyName = "altitude")]
public string Altitude { get; set; }
[JsonProperty(PropertyName = "on_ground")]
public string IsOnGround { get; set; }
[JsonProperty(PropertyName = "velocity")]
public string Velocity { get; set; }
[JsonProperty(PropertyName = "heading")]
public string Heading { get; set; }
[JsonProperty(PropertyName = "vertical_rate")]
public string VerticalRate { get; set; }
[JsonProperty(PropertyName = "sensors")]
public string[] Sensors { get; set; }
}
Second problem: the objects inside the "states" array does not contain field names. But just because their documentation gave the names of the fields, I've put them in the JsonProperty
anyway.
The way I am doing is like this (in my main app):
var apiResponse = _rest.GetResponse("https://opensky-network.org/api/states/all");
//first thing I tried
var res = JsonConvert.DeserializeObject<IDictionary<string, FlightInformation>>(apiResponse);
//second thing I tried
var res = JsonConvert.DeserializeObject<FlightInformation>(apiResponse);
//third thing
var res = JsonConvert.DeserializeObject<FlightInformation>(apiResponse).FlightDetails;
//and fourth thing...
var res = JsonConvert.DeserializeObject<List<FlightDetail>>(apiResponse);
At every one of them, the error I am getting is quite different. The result with the dictionary is like this:
Error converting value 1484344840 to type 'FlightManager.DAL.FlightInformation'.
The result with the second one:
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'FlightManager.DAL.FlightDetail' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
The result with the third one is same as second one.
The result with the last (fourth) one is:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[FlightManager.DAL.FlightDetail]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
As I said, I don't really need to use Newtonsoft.JSON as I am only experimenting... What could I be doing wrong?
Cheers.