2011-08-26 59 views
0
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]; 
    } 

我正在使用上面的代码进行json解析。当编译器出现在这行为什么它终止?

NSString * Resptxt = [response objectForKey:@“text”];

然后编译终止,给这个错误

*终止应用程序由于未捕获的异常 'NSInvalidArgumentException',原因是: ' - [NSCFString objectForKey:]:无法识别的选择发送到实例0x6166490'

包含的状态是

'message':{'code':0,'text':'OK','version':'1.1.0'}}

这是什么问题?我如何改正它?

+0

你可以在这里粘贴包含状态... – Robin

+0

试试这个NSString * Resptxt = [response valueForKey:@“text”]; –

+0

编译器不会终止您的应用程序。它只是编译。 (附注) –

回答

0

这看起来不像编译器遇到的错误。相反,这是运行应用程序时遇到的错误。

它表示您已将消息objectForKey:发送到字符串实例,即响应是字符串实例。但是,该消息通常发送给NSDictionary的实例。

我假设JSON响应的结构与预期不同。特别是,它比您的代码假设的嵌套更深。

请发布完整的JSON答案,以便我们可以给您更具体的帮助。

+0

我同意Codo,json是进一步调试绝对必要的。 –

0

响应变量作为NSString(NSCFString)而不是NSDictionary返回。 NSString没有objectForKey方法,这就是为什么你调用时该应用程序崩溃的原因NSString *Resptxt =[response objectForKey:@"text"];

0

它看起来像是发生的是状态内的对象不是词典,而是字符串。你得到的错误是说当你调用objectForKey时:nscfstring不响应它。

相关问题