Virat Naithani
Virat Naithani

Reputation: 783

problem in parsing JSON response in iphone

i am getting json data from this link

now i want data from "html_instructions": part

NSDictionary *result = [stringtext JSONValue];
NSLog(@"here");
NSArray *resultarr = [result objectForKey:@"routes"];
NSString *string;

for(NSDictionary *di in resultarr){
    NSLog(@"for loop");
    string = [[di objectForKey:@"legs"] objectForKey:@"steps"];
}

but after printing "for loop" in console it is throwing an exception

2011-05-20 16:16:26.997 speedymap[759:207] -[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60

2011-05-20 16:16:26.999 speedymap[759:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0x62a7d60'

please help me i want html_instructiuons field value

Upvotes: 2

Views: 1341

Answers (2)

karim
karim

Reputation: 15599

Check if resultarr and di really contains anything or not.

NSDictionary *result = [stringtext JSONValue];
NSLog(@"here");
NSArray *resultarr = [result objectForKey:@"routes"];
CFShow(resultarr);
NSString *string;

for(NSDictionary *di in resultarr){
    CFShow(di);
    NSLog(@"for loop");
    NSArray *tempArr = [[di objectForKey:@"legs"] objectForKey:@"steps"];
}

Upvotes: 3

Nithin
Nithin

Reputation: 6475

http://jsonviewer.stack.hu/#http://maps.googleapis.com/maps/api/directions/json?origin=delhi&destination=noida&waypoints=&sensor=true

Check the viewer and set the values accordingly. [di objectForKey:@"legs"] returns an array. the first object of that array is a dictionary which has the key steps. But that too returns another array.

Upvotes: 1

Related Questions