2011-08-26 48 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不響應它。