【发布时间】:2019-10-09 08:35:00
【问题描述】:
虽然我已经在老学校开发了几年,但我需要一些帮助来了解如何将此 json 显示到列表视图。
从 URL 返回的 JSON
[
{
"LastUpdated":"09/10/2019 09:08:02",
"Latitude":-33.917713,
"Longitude":18.431315,
"PlannedEndDate":"",
"Reported":"09/10/2019 09:00:38",
"StartDate":"09/10/2019 09:00:38",
"ID":"WC-ATMS-196763",
"RegionName":"Western Cape",
"CountyName":null,
"Severity":"none",
"RoadwayName":"Table Bay Blvd",
"DirectionOfTravel":"Inbound",
"Description":null,
"Location":"0.0 km after N2 Elevated Freeway",
"LanesAffected":null,
"LanesStatus":null,
"LcsEntries":null,
"NavteqLinkId":"771421441",
"PrimaryLocation":null,
"SecondaryLocation":null,
"FirstArticleCity":null,
"SecondCity":null,
"EventType":"TrafficConditions",
"EventSubType":"Mainline Congestion",
"MapEncodedPolyline":null
},
{
"LastUpdated":"09/10/2019 09:30:02",
"Latitude":-26.0378,
"Longitude":28.016144,
"PlannedEndDate":"",
"Reported":"09/10/2019 08:08:25",
"StartDate":"09/10/2019 08:08:25",
"ID":"GP-ATMS-207804",
"RegionName":"Gauteng",
"CountyName":null,
"Severity":"minor",
"RoadwayName":"N1",
"DirectionOfTravel":"Southbound",
"Description":null,
"Location":"0.7 km before Ramp from William Nicol I/C",
"LanesAffected":null,
"LanesStatus":null,
"LcsEntries":null,
"NavteqLinkId":"837759156",
"PrimaryLocation":null,
"SecondaryLocation":null,
"FirstArticleCity":null,
"SecondCity":null,
"EventType":"TrafficConditions",
"EventSubType":"Ramp Congestion",
"MapEncodedPolyline":null
},
{
"LastUpdated":"09/10/2019 09:52:20",
"Latitude":-33.944828,
"Longitude":18.472132,
"PlannedEndDate":"",
"Reported":"09/10/2019 09:49:42",
"StartDate":"09/10/2019 09:49:42",
"ID":"WC-ATMS-196773",
"RegionName":"Western Cape",
"CountyName":null,
"Severity":"none",
"RoadwayName":"N2",
"DirectionOfTravel":"Inbound",
"Description":null,
"Location":"0.0 km after Ramp to Mowbray Main",
"LanesAffected":null,
"LanesStatus":null,
"LcsEntries":null,
"NavteqLinkId":"771096842",
"PrimaryLocation":null,
"SecondaryLocation":null,
"FirstArticleCity":null,
"SecondCity":null,
"EventType":"AccidentsAndIncidents",
"EventSubType":"Shoulder Lane",
"MapEncodedPolyline":null
}
]
事件对象
public class Events
{
public string LastUpdated { get; set; }
public float Latitude { get; set; }
public float Longitude { get; set; }
public string PlannedEndDate { get; set; }
public string Reported { get; set; }
public string StartDate { get; set; }
public string ID { get; set; }
public string RegionName { get; set; }
public object CountyName { get; set; }
public string Severity { get; set; }
public string RoadwayName { get; set; }
public string DirectionOfTravel { get; set; }
public object Description { get; set; }
public string Location { get; set; }
public string LanesAffected { get; set; }
public object LanesStatus { get; set; }
public object LcsEntries { get; set; }
public string NavteqLinkId { get; set; }
public object PrimaryLocation { get; set; }
public object SecondaryLocation { get; set; }
public object FirstArticleCity { get; set; }
public object SecondCity { get; set; }
public string EventType { get; set; }
public string EventSubType { get; set; }
public object MapEncodedPolyline { get; set; }
}
代码
ObservableCollection<Events> events = new ObservableCollection<Events>();
Events events = JsonConvert.DeserializeObject<Events>(json);
lvEvents.ItemsSource = events;
xaml
<ListView x:Name="lvEvents" ItemsSource="{Binding Events}" >
<ListView.ItemTemplate>
<DataTemplate>
<Label Text="{Binding RegionName}" ></Label>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
我收到错误
无法将当前 JSON 数组(例如 [1,2,3])反序列化为类型“Project.View.Events”,因为该类型需要 JSON 对象(例如 {"name":"value"})才能正确反序列化. 要修复此错误,请将 JSON 更改为 JSON 对象(例如 {"name":"value"})或将反序列化类型更改为数组或实现集合接口的类型(例如 ICollection、IList),例如可以从 JSON 数组反序列化。 JsonArrayAttribute 也可以添加到类型中以强制它从 JSON 数组中反序列化。 路径'',第 1 行,位置 1。
【问题讨论】:
-
能否格式化您的 json 以便其可读?
-
JsonConvert.DeserializeObject<Events>(json)应该是JsonConvert.DeserializeObject<ObservableCollection<Events>>(json); -
您的 json 包含事件列表
Newtonsoft.Json.JsonConvert.DeserializeObject<List<Events>>(json); -
您是否阅读了错误消息并仔细检查了 JSON 的外观以及反序列化的预期内容?