晚上好!
我需要一些帮助来解析Google Directions中的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
},
(...)
有人可以帮忙吗?
提前致谢!
最佳答案
嗨,我希望使用此代码可能对您有所帮助
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数据的所有信息
祝好运
关于objective-c - 解析JSON时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7989173/