I am currently writing a Xamarin forms application. The issue I am facing is that I am receiving a Json Response from my API, and storing it in a var JsonReponse. The code is as follows;
string UserId = Settings.CallUserId;
var client = new RestClient("http://blablablaAPiCall" UserId);
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Content-Type", "application/json");
var body = @"";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
var JsonResponse = JsonConvert.DeserializeObject<Flights[]>(response.Content);
FlightsCollection = new ObservableCollection<Flights>
{
new Flights() {Id="1", SectorStart="1", Registration="A123",
Departure="Jhb", OffBlockTime="1242", Arrival="EC", OnBlockTime="1342",
TotalFlightTime="0100", InstrumentTime="00" }
};
The class I made looks like this; public class Flights {
public string Id { get; set; }
public string UserId { get; set; }
public string EntryDate { get; set; }
public string IsMasterId { get; set; }
public string SectorStart { get; set; }
public string SectorEnd { get; set; }
public string AircraftType { get; set; }
public string Registration { get; set; }
public string PicName { get; set; }
public string PilotFunction { get; set; }
public string Departure { get; set; }
public string Arrival { get; set; }
public string OffBlockTime { get; set; }
public string OnBlockTime { get; set; }
public string TotalFlightTime { get; set; }
public string NightLanding { get; set; }
public string Instructor { get; set; }
public string Simulator { get; set; }
public string NightTime { get; set; }
public string DayTime { get; set; }
public string InstrumentTime { get; set; }
public string ApproachType { get; set; }
public string NavAid { get; set; }
public string Place { get; set; }
public string FinalRemarks { get; set; }
}
At the top I declared an Obvservable Collection, public ObservableCollection FlightsCollection { get; set; }
So now the end goal would be for me to set my json response = Observable collection, as you can see I have done it statically, but now how do I do it with my json response?
My json response looks like this if needed,
[
{
"ID": 15,
"UserID": 10,
"EntryDate": "2022/01/02 2:34:20 PM",
"IsMasterID": true,
"SectorStart": 1,
"SectorEnd": 1,
"Aircraft_Type": "Unclassified",
"Registration": "aircraft reg",
"PICName": "Jordan Watts",
"PilotFunction": "PIC",
"Departure": "Jhb",
"Arrival": "EC",
"OffBlockTime": "00:00",
"OnBlockTime": "01:15",
"TotalFlightTime": "01:15",
"NightLanding": true,
"Instructor": true,
"Simulator": true,
"NightTime": "01:15",
"DayTime": "00:00",
"InstrumentTime": "0000",
"ApproachType": "VOR",
"NavAid": "nav",
"Place": "here",
"FinalRemarks": "this flight was cool"
},
{
"ID": 16,
"UserID": 10,
"EntryDate": "2022/01/02 2:44:10 PM",
"IsMasterID": true,
"SectorStart": 1,
"SectorEnd": 3,
"Aircraft_Type": "Unclassified",
"Registration": "aircraft reg",
"PICName": "Jordan Watts ",
"PilotFunction": "PICUS",
"Departure": "JHb",
"Arrival": "EC",
"OffBlockTime": "00:00",
"OnBlockTime": "01:00",
"TotalFlightTime": "01:00",
"NightLanding": true,
"Instructor": true,
"Simulator": true,
"NightTime": "01:00",
"DayTime": "00:00",
"InstrumentTime": "0000",
"ApproachType": "VOR",
"NavAid": "nav aid",
"Place": "here",
"FinalRemarks": "rhrbdidbf"
},
{
"ID": 18,
"UserID": 10,
"EntryDate": "2022/01/02 2:53:56 PM",
"IsMasterID": true,
"SectorStart": 1,
"SectorEnd": 2,
"Aircraft_Type": "Unclassified",
"Registration": "reg",
"PICName": "Jordan Watts ",
"PilotFunction": "PICUS",
"Departure": "Jhb",
"Arrival": "EC",
"OffBlockTime": "00:00",
"OnBlockTime": "02:15",
"TotalFlightTime": "02:15",
"NightLanding": true,
"Instructor": true,
"Simulator": true,
"NightTime": "02:15",
"DayTime": "00:0",
"InstrumentTime": "0000",
"ApproachType": "VOR",
"NavAid": "here",
"Place": "here 2",
"FinalRemarks": "flight 1 of 2 "
}
]
CodePudding user response:
ObservableCollection
accepts an IEnumerable
which DeserializeObject
can generate. Try this:
FlightsCollection = new ObservableCollection<Flights>(JsonConvert.DeserializeObject<List<Flights>>(response.Content))
CodePudding user response:
if you want to have only some properties , you can create subclass accoring to your needs
FlightBase[] flights = JsonConvert.DeserializeObject<FlightBase[]>(response.Content);
ObservableCollection<FlightBase> FlightsCollection = new ObservableCollection<FlightBase>(flights);
classes
public class FlightBase
{
public int ID { get; set; }
public int SectorStart { get; set; }
public string Registration { get; set; }
public string Departure { get; set; }
public string Arrival { get; set; }
public string OffBlockTime { get; set; }
public string OnBlockTime { get; set; }
public string TotalFlightTime { get; set; }
public string InstrumentTime { get; set; }
}
public class Flight:FlightBase
{
public int UserID { get; set; }
public string EntryDate { get; set; }
public bool IsMasterID { get; set; }
public int SectorEnd { get; set; }
public string Aircraft_Type { get; set; }
public string PICName { get; set; }
public string PilotFunction { get; set; }
public bool NightLanding { get; set; }
public bool Instructor { get; set; }
public bool Simulator { get; set; }
public string NightTime { get; set; }
public string DayTime { get; set; }
public string ApproachType { get; set; }
public string NavAid { get; set; }
public string Place { get; set; }
public string FinalRemarks { get; set; }
}