我想解析这个网址:https ://data.cityofchicago.org/api/views/xzkq-xp2w/rows.json?search=rahm并在模拟器中显示数据。
我的代码是:
WebClient webClient = new WebClient();
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(downloadAboutCompleted);
webClient.OpenReadAsync(new Uri("https://data.cityofchicago.org/api/views/xzkq-xp2w/rows.json?search=rahm"), c);
private void downloadAlbumCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
using (StreamReader httpwebStreamReader = new StreamReader(e.Result))
{
var results = httpwebStreamReader.ReadToEnd();
System.Diagnostics.Debug.WriteLine(results):
var json = JObject.Parse(results);
System.Diagnostics.Debug.WriteLine(json):
foreach (JObject array in json["meta"]["view"])
{
JObject obj = JObject.Parse(array.ToString());
string id= (string)obj["id"];
string name= (string)obj["name"];
MessageBox.Show(name);
}
}
}
}
但我在以下行中遇到错误:
foreach (JObject array in json["meta"]["view"])
错误是:An exception of type 'System.InvalidCastException' occurred in PhoneApp1.DLL but was not handled in user code
提前致谢。