【发布时间】: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
},
(...)
有人可以帮忙吗?! 提前致谢!
【问题讨论】:
标签: objective-c ios json