C# JSON解析以检索和显示文本框中的属性值(WIndows Phone 8)
我试图从URL解析JSON,检索每个属性的数据值,并将其显示在文本框中。但我面临着这个错误: “Newtonsoft.Json.JsonReaderException类型的异常在Newtonsoft.Json.DLL中发生,但未在用户代码中处理”读取整数时出错。意外标记:星光。路径“BusRoute[0]。步骤 这是我的密码:C# JSON解析以检索和显示文本框中的属性值(WIndows Phone 8),c#,json,windows-phone-8,C#,Json,Windows Phone 8,我试图从URL解析JSON,检索每个属性的数据值,并将其显示在文本框中。但我面临着这个错误: “Newtonsoft.Json.JsonReaderException类型的异常在Newtonsoft.Json.DLL中发生,但未在用户代码中处理”读取整数时出错。意外标记:星光。路径“BusRoute[0]。步骤 这是我的密码: // Create webclient. WebClient client = new WebClient();
// Create webclient.
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
client.DownloadStringAsync(new Uri("http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32CvLC4RMVkzEFYjxqyti1KhByvEacEdMWBpCuSSQ+IFRT84QjGPBCuz/cBom8PfSm3GjEsGc8PkdEEOEr&sl=39167.4524,35518.8625&el=28987.5163,33530.5653&startstop=&endstop=&walkdist=300&mode=bus&routeopt=cheapest&retgeo=true&maxsolns=1&callback="));
}
/*public void loadHTMLCallback(Object sender, DownloadStringCompletedEventArgs e)
{
string content = e.Result;
tb_json.Text = content ;
}*/
public class BusRoute
{
public int SOLUTION { get; set; }
public string DURATION { get; set; }
public string TOTALCARD { get; set; }
public string TOTALCASH { get; set; }
public string TOTALDISTANCE { get; set; }
public int STEPS { get; set; }
public string TYPE { get; set; }
public string SERVICETYPE { get; set; }
public string SERVICEID { get; set; }
public string NUMBEROFSTOP { get; set; }
public string BOARDID { get; set; }
public string BOARDDESC { get; set; }
public string BOARDDIST { get; set; }
public string ALIGHTID { get; set; }
public string ALIGHTDESC { get; set; }
public string ALIGHTDIST { get; set; }
public int TOTALSTOPS { get; set; }
public string PATH { get; set; }
}
public class RootObject
{
public List<BusRoute> BusRoute { get; set; }
}
void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);
foreach (var route in rootObject.BusRoute)
{
int t = route.TOTALSTOPS;
string totalStops = t.ToString();
tb_test.Text = "Total number of stops: " + totalStops;
}
}
//创建webclient。
WebClient客户端=新的WebClient();
client.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(webClient\u DownloadStringCompleted);
client.DownloadStringAsync(新Uri(“http://www.onemap.sg/publictransportation/service1.svc/routesolns?token=qo/s2TnSUmfLz+32cvlc4rmvkzefyjxqyti1khbyBeacedwbpcussq+IFRT84QjGPBCuz/cbom8pfsm3gjesgc8pkdeeer&sl=39167.452435518.8625&el=28987.516333530.5653&startstop=&endstop=&walkdist=300&mode=bus&routeopt=chest&retgeo=true&maxsolns=1&callback=“));
}
/*public void loadHTMLCallback(对象发送方,下载StringCompletedEventArgs e)
{
字符串内容=e.结果;
tb_json.Text=内容;
}*/
公营巴士路线
{
公共int解决方案{get;set;}
公共字符串持续时间{get;set;}
公共字符串TOTALCARD{get;set;}
公共字符串TOTALCASH{get;set;}
公共字符串TOTALDISTANCE{get;set;}
公共int步骤{get;set;}
公共字符串类型{get;set;}
公共字符串服务类型{get;set;}
公共字符串SERVICEID{get;set;}
公共字符串NUMBEROFSTOP{get;set;}
公共字符串BOARDID{get;set;}
公共字符串BOARDDESC{get;set;}
公共字符串BOARDDIST{get;set;}
公共字符串ALIGHTID{get;set;}
公共字符串ALIGHTDESC{get;set;}
公共字符串ALIGHTDIST{get;set;}
公共整数{get;set;}
公共字符串路径{get;set;}
}
公共类根对象
{
公共列表总线路由{get;set;}
}
void webClient_DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
var rootObject=JsonConvert.DeserializeObject(例如Result);
foreach(rootObject.BusRoute中的var路由)
{
int t=路线。总站数;
字符串totalStops=t.ToString();
tb_test.Text=“总停车次数:”+总停车次数;
}
}
请给我一些关于如何检索属性并将其显示在文本框中的指导。谢谢大家! 我尝试使用您的json url: (将代币放在公共互联网上是否安全?) 从生成的类
公共类步骤
{
公共字符串步骤{get;set;}
公共字符串类型{get;set;}
公共字符串服务类型{get;set;}
公共字符串ServiceID{get;set;}
公共字符串NumberOfStop{get;set;}
公共字符串BoardId{get;set;}
公共字符串BoardDesc{get;set;}
公共字符串BoardDist{get;set;}
公共字符串AlightId{get;set;}
公共字符串AlightDesc{get;set;}
公共字符串AlightDist{get;set;}
}
公营巴士路线
{
公共字符串解决方案{get;set;}
公共字符串持续时间{get;set;}
公共字符串TotalCard{get;set;}
公共字符串TotalCash{get;set;}
公共字符串TotalDistance{get;set;}
公共列表步骤{get;set;}
公共字符串TotalStops{get;set;}
公共列表路径{get;set;}
}
公共类根对象
{
公共列表总线路由{get;set;}
}
var rootObject=JsonConvert.DeserializeObject(例如Result);反序列化后可以打印rootObject吗?不,我不能@Lee GaryCan你能调试吗?例如,创建桌面项目并单步执行?您可以快速观察或做些什么,看看正在反序列化什么吗?您得到的错误是,当您在模型类中将集合/数组作为单值属性类型(整数)返回时,JSON将集合/数组作为STEPS
属性的值返回。按照@leegray的建议修复您的模型(使用json2csharp生成模型类非常方便,可以避免此类错误)。
public class STEP
{
public string STEP { get; set; }
public string type { get; set; }
public string ServiceType { get; set; }
public string ServiceID { get; set; }
public string NumberOfStop { get; set; }
public string BoardId { get; set; }
public string BoardDesc { get; set; }
public string BoardDist { get; set; }
public string AlightId { get; set; }
public string AlightDesc { get; set; }
public string AlightDist { get; set; }
}
public class BusRoute
{
public string Solution { get; set; }
public string Duration { get; set; }
public string TotalCard { get; set; }
public string TotalCash { get; set; }
public string TotalDistance { get; set; }
public List<STEP> STEPS { get; set; }
public string TotalStops { get; set; }
public List<List<string>> PATH { get; set; }
}
public class RootObject
{
public List<BusRoute> BusRoute { get; set; }
}
var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result); <- this works