解析 JSON 时出错
Posted
技术标签:
【中文标题】解析 JSON 时出错【英文标题】:Error parsing JSON 【发布时间】:2011-11-03 00:59:57 【问题描述】:晚上好!
我需要一些帮助来解析来自 Google 方向的 JSON。 我有以下代码来获取 html_instructions 值:
NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:
[NSURL URLWithString:@"http://maps.google.com/maps/api/directions/json?origin=-15.802737,-47.87963&destination=-15.851783,-47.954593&sensor=true"]];
// Create a dictionary from the JSON string
NSDictionary *results = [myRawJson JSONValue];
NSArray *resultarr = [results objectForKey:@"routes"];
NSString *string;
//NSArray *subarray;
for(NSDictionary *di in resultarr)
NSLog(@"loop");
NSDictionary *subdic = [[di objectForKey:@"legs"] objectForKey:@"steps"];
string = [subdic objectForKey:@"html_instructions"];
NSLog(@"%@",string);
(...)
当我运行它时,我得到以下错误
-[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0x6d9b3e0 2011-11-02 22:53:02.110 APSplitSample[1470:11603] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[__NSArrayM objectForKey:]:无法识别的选择器发送到实例 0x6d9b3e0 '
这是 JSON(可以看到完整运行上面的 url):
"routes" : [
"bounds" :
"northeast" :
"lat" : -15.79760,
"lng" : -47.879710
,
"southwest" :
"lat" : -15.874160,
"lng" : -47.958310
,
"copyrights" : "Dados cartográficos ©2011 MapLink",
"legs" : [
"distance" :
"text" : "16,0 km",
"value" : 15989
,
"duration" :
"text" : "16 minutos",
"value" : 945
,
"end_address" : "Estr. Epia - Candangolândia, Brasília - DF, Brasil",
"end_location" :
"lat" : -15.851770,
"lng" : -47.95470
,
"start_address" : "Via de Ligação Se/ne - Brasília, DF, Brasil",
"start_location" :
"lat" : -15.802960,
"lng" : -47.879710
,
"steps" : [
"distance" :
"text" : "0,6 km",
"value" : 625
,
"duration" :
"text" : "1 min",
"value" : 59
,
"end_location" :
"lat" : -15.800940,
"lng" : -47.885150
,
"html_instructions" : "Siga na direção \u003cb\u003eoeste\u003c/b\u003e na \u003cb\u003eVia de Ligação Se/ne\u003c/b\u003e em direção à \u003cb\u003eVia Bs S Um\u003c/b\u003e",
"polyline" :
"points" : "nom_BdofcHa@hB[rACRId@Kf@g@|BOl@AFKb@Kd@i@~BaArCKZiA`DKVCFGLCFKX"
,
"start_location" :
"lat" : -15.802960,
"lng" : -47.879710
,
"travel_mode" : "DRIVING"
,
"distance" :
"text" : "0,1 km",
"value" : 101
,
(...)
有人可以帮忙吗?! 提前致谢!
【问题讨论】:
【参考方案1】:您好,我希望使用此代码可能对您有所帮助
NSDictionary *mainDict=[requeststring JSONValue];
//NSLog(@"maindict values is %d",[mainDict count]);
NSArray *routesArray=[mainDict objectForKey:@"routes"];
if ([routesArray count]>0)
NSDictionary *routeDict=[routesArray objectAtIndex:0];
NSArray *legsarray=[routeDict objectForKey:@"legs"];
NSDictionary *legsdict=[legsarray objectAtIndex:0];
NSDictionary *distDict=[legsdict objectForKey:@"distance"];
NSString *distncestring=[distDict objectForKey:@"text"];
NSDictionary *timeDict=[legsdict objectForKey:@"duration"];
NSString *time=[timeDict objectForKey:@"text"];
NSDictionary *startlatdict=[legsdict objectForKey:@"start_location"];
float latt=[[startlatdict objectForKey:@"lat"] floatValue];
float long=[[startlatdict objectForKey:@"lng"] floatValue];
NSArray *stepsarray=[legsdict objectForKey:@"steps"];
for (int i=0; i<[stepsarray count]; i++)
NSDictionary *stepsdict=[stepsarray objectAtIndex:i];
NSDictionary *endlattdict=[stepsdict objectForKey:@"end_location"];
float lang1=[[endlattdict objectForKey:@"lat"] floatValue];
float long1=[[endlattdict objectForKey:@"lng"] floatValue];
NSString *instStr =[stepsdict objectForKey:@"html_instructions"];
NSLog(@"html_instructions%@",instStr);
此代码可帮助您获取 google map json 数据的所有信息
祝你好运
【讨论】:
我在哪里可以获得类 ASIFormDataRequest? 将 ASIHttpRequest 文件导入到您的项目中【参考方案2】:结果:
[di objectForKey:@"legs"]
似乎是另一个数组,而不是字典。所以当你添加时
[[di objectForJet:@"legs"] objectForKey:@"steps"]
您正在将 objectForKey 消息发送到 NSArray。
试试这样的:
NSArray *legs = [di objectForKey@"legs];
for (NSDictionary *subdic in legs)
string = [subdic objectForKey:@"html_instructions"];
NSLog(@"%@",string);
【讨论】:
以上是关于解析 JSON 时出错的主要内容,如果未能解决你的问题,请参考以下文章