envyM6 envyM6 - 6 months ago 61
JSON Question

Unable to Deserialize JSON in an UWP app

Hi all I'm trying to parse a JSON into c# classes using Newtonsoft Json. The JSON I'm trying to parse is as follows-

[
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [ ],
"created": "2016-05-10T15:41:47.997",
"modified": "2016-05-10T15:41:47.997",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": [ ]
}
],
"routeSections": [ ],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Bakerloo&serviceTypes=Regular"
}
]
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "central",
"name": "Central",
"modeName": "tube",
"disruptions": [ ],
"created": "2016-05-10T15:41:48.45",
"modified": "2016-05-10T15:41:48.45",
"lineStatuses": [
{
"$type": "Tfl.Api.Presentation.Entities.LineStatus, Tfl.Api.Presentation.Entities",
"id": 0,
"statusSeverity": 10,
"statusSeverityDescription": "Good Service",
"created": "0001-01-01T00:00:00",
"validityPeriods": [ ]
}
],
"routeSections": [ ],
"serviceTypes": [
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Regular",
"uri": "/Line/Route?ids=Central&serviceTypes=Regular"
}
]
}
]


The JSON is retrieved via this following code block-

public static async Task<List<TubeAPI>> GetTubeStatusAsync()
{
string url =
String.Format("https://api.tfl.gov.uk/Line/central,victoria/Status/detail=true&app_id=&app_key=");
HttpClient http = new HttpClient();
var response = await http.GetAsync(url);
var jsonMessage = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<List<TubeAPI>>(jsonMessage);
return result;
}


The classes I'm trying to de serialize into are as follows-

public class TubeAPI
{
public RootArray[] Property1 { get; set; }
}

public class RootArray
{
public string type { get; set; }
public string id { get; set; }
public string name { get; set; }
public string modeName { get; set; }
public object[] disruptions { get; set; }
public DateTime created { get; set; }
public DateTime modified { get; set; }
public Linestatus[] lineStatuses { get; set; }
public object[] routeSections { get; set; }
public Servicetype[] serviceTypes { get; set; }
}

public class Linestatus
{
public string type { get; set; }
public int id { get; set; }
public int statusSeverity { get; set; }
public string statusSeverityDescription { get; set; }
public DateTime created { get; set; }
public object[] validityPeriods { get; set; }
}

public class Servicetype
{
public string type { get; set; }
public string name { get; set; }
public string uri { get; set; }
}


I've set the debugger at
var result
to debug and see if the parsing goes through and I have data in the classes. But the classes are not populated!.
enter image description here

As you can see the
Property1
field is null. I'm confused, what am I doing wrong here?

Answer

You're desserializing as a List<TubeAPI>. You should instead be deserializing as a List<RootArray>, because that's the structure that corresponds to your JSON. You've got an array of RootArray, not an array of TubeAPI. If you wrap all the JSON in quotes then you'd have a singular TubeAPI.

var result = JsonConvert.DeserializeObject<List<RootArray>>(jsonMessage);