OK, I'll try to be short and specific. I am getting a JSON string from an public API that looks like this: (short version of it, took only 2 samples)
[{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "bakerloo",
"name": "Bakerloo",
"modeName": "tube",
"disruptions": [],
"created": "2017-03-16T15:56:01.01Z",
"modified": "2017-03-16T15:56:01.01Z",
"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"
}
],
"crowding": {
"$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
}
},
{
"$type": "Tfl.Api.Presentation.Entities.Line, Tfl.Api.Presentation.Entities",
"id": "central",
"name": "Central",
"modeName": "tube",
"disruptions": [],
"created": "2017-03-16T15:56:01.01Z",
"modified": "2017-03-16T15:56:01.01Z",
"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"
},
{
"$type": "Tfl.Api.Presentation.Entities.LineServiceTypeInfo, Tfl.Api.Presentation.Entities",
"name": "Night",
"uri": "/Line/Route?ids=Central&serviceTypes=Night"
}
],
"crowding": {
"$type": "Tfl.Api.Presentation.Entities.Crowding, Tfl.Api.Presentation.Entities"
}
}]
so far so good, below I'll paste the relevant code that I'm using and trying to deserialize this JSON string into C# classes that I got from json2csharp free online service. The relevant code where I'm trying to achieve this is:
public async static Task<tubeStatusRootObject> GetTubeStatus(string url)
{
var http = new HttpClient();
var response = await http.GetAsync(url);
var result = await response.Content.ReadAsStringAsync(); //This is working
var deserializer = new DataContractJsonSerializer(typeof(tubeStatusRootObject));
var ms = new MemoryStream(Encoding.UTF8.GetBytes(result));
var data = (tubeStatusRootObject)deserializer.ReadObject(ms);
return data; //all "data" properties are null
}
as you can read the comments above, all I'm getting inside data before return is null.
The classes that were generated by json2csharp looks like this:
[DataContract]
public class Disruption
{
[DataMember]
public string type { get; set; }
[DataMember]
public string category { get; set; }
[DataMember]
public string categoryDescription { get; set; }
[DataMember]
public string description { get; set; }
[DataMember]
public string additionalInfo { get; set; }
[DataMember]
public string created { get; set; }
[DataMember]
public List<object> affectedRoutes { get; set; }
[DataMember]
public List<object> affectedStops { get; set; }
[DataMember]
public string closureText { get; set; }
[DataMember]
public bool? isWholeLine { get; set; }
}
[DataContract]
public class LineStatus
{
[DataMember]
public string type { get; set; }
[DataMember]
public int id { get; set; }
[DataMember]
public int statusSeverity { get; set; }
[DataMember]
public string statusSeverityDescription { get; set; }
[DataMember]
public string created { get; set; }
[DataMember]
public List<object> validityPeriods { get; set; }
[DataMember]
public string lineId { get; set; }
[DataMember]
public string reason { get; set; }
[DataMember]
public Disruption disruption { get; set; }
}
[DataContract]
public class ServiceType
{
[DataMember]
public string type { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string uri { get; set; }
}
[DataContract]
public class Crowding
{
[DataMember]
public string type { get; set; }
}
[DataContract]
public class tubeStatusRootObject
{
[DataMember]
public string type { get; set; }
[DataMember]
public string id { get; set; }
[DataMember]
public string name { get; set; }
[DataMember]
public string modeName { get; set; }
[DataMember]
public List<object> disruptions { get; set; }
[DataMember]
public string created { get; set; }
[DataMember]
public string modified { get; set; }
[DataMember]
public List<LineStatus> lineStatuses { get; set; }
[DataMember]
public List<object> routeSections { get; set; }
[DataMember]
public List<ServiceType> serviceTypes { get; set; }
[DataMember]
public Crowding crowding { get; set; }
}
obviously I just added the [DataContract]'s and [DataMember]'s there. Anyone with idea of what I am doing wrong and could help me
I've followed the example from Channel9
Please don't mark duplicate, as I've found a lot similar questions, some using newtonsoft json but I could not implement the solutions from there into my example
