Why it is terminating?
responseData = [[NSMutableData data] retain];
NSString *requestString = [NSString stringWithFormat:@"http://api.yelp.com/business_review_search?term=&lat=%f&long=%f&radius=10&limit=20&ywsid=XXXXXXXXXX&category=%@",[[ListofLat objectAtIndex:i] floatValue], [[ListofLong objectAtIndex:i] floatValue],appDelegate.categoryField] ;
//NSURL *url = [[NSURL alloc] initWithString:requestString];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:requestString]];
/* NSData *tempData =[[NSData alloc] initWithContentsOfURL:url];
NSString *Str = [[NSString alloc] initWithData:tempData encoding:NSUTF8StringEncoding];
NSLog(@"%@",Str);*/
NSURLResponse *response = nil;
NSError *error = nil;
//getting the data
NSData *newData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//json parse
NSString *responseString开发者_开发技巧 = [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
NSDictionary *jsonObject = [responseString JSONValue];
//Accessing JSON content
NSLog(@"message : %@", [jsonObject objectForKey:@"message"] );
NSArray *status = [jsonObject objectForKey:@"message"] ;
NSLog(@"message : %@", status );
for(NSDictionary *response in status)
{
NSString *Resptxt =[response objectForKey:@"text"];
txtStatus=[Resptxt copy];
}
if([txtStatus isEqualToString : @"OK"])
{
UIAlertView *info = [[UIAlertView alloc] initWithTitle:@"Info" message: @"The request completed without error. " delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[info show];
[info release];
}
I am using above code for json parsing. when compiler comes on this line
NSString *Resptxt =[response objectForKey:@"text"];
then compiler terminate and give this error
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString objectForKey:]: unrecognized selector sent to instance 0x6166490'
contains of status is
'message': {'code': 0, 'text': 'OK', 'version': '1.1.0'}}
What is problem in this? How i correct it?
This doesn't look like an error the compiler encounters. Rather it's an error you run into when running your app.
It indicates that you sent to message objectForKey:
to a string instance, i.e. response is a string instance. However, this message is normally sent to instance of NSDictionary
.
I assume that the JSON response has a different structure than you expect. In particular, it is more deeply nested than your code assumes.
Please post the full JSON answer so we can give you more specific help.
The response variable is being returned as a NSString (NSCFString) rather than an NSDictionary. NSString doesn't have a objectForKey method which is why the app crashes with that error when you are calling NSString *Resptxt =[response objectForKey:@"text"];
What it looks like is happening is the objects within status are not dictionaries but strings. The error your getting is saying that when you call the objectForKey: that nscfstring does not respond to it.
精彩评论