大家好我正在尝试使用Newtonsoft Json将JSON解析为c#类。我试图解析的JSON如下 -
[
{
"$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"
}
]
}
]
通过以下代码块检索JSON -
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;
}
我想要序列化的类如下 -
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; }
}
我已将调试器设置为var result
以进行调试,并查看解析是否通过,并且我在类中有数据。但这些课程没有填充!
如您所见,Property1
字段为空。我很困惑,我在这里做错了什么?
答案 0 :(得分:1)
您正在反序列化为List<TubeAPI>
。您应该反序列化为List<RootArray>
,因为这是与您的JSON对应的结构。你有一个RootArray
数组,而不是TubeAPI
数组。如果您将所有JSON包装在引号中,那么您将拥有单数TubeAPI
。
var result = JsonConvert.DeserializeObject<List<RootArray>>(jsonMessage);