2015-08-03 42 views
0

你好,我想解析一個JSON,但每次我嘗試我失敗。我已經嘗試過最近使用過的這種方法,並且在除了這個之外的所有其他項目中工作。 json是有效的。解析NSCFString objectForKeyedSubscript錯誤

NSString *post = [NSString stringWithFormat:@"x=2&y=3&z=1"]; 

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[[NSURL alloc] initWithString:@"link-to-php.php"]]; 
    [request setHTTPMethod:@"POST"]; 
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPBody:postData]; 


    NSURLResponse *requestResponse; 
    NSData *requestHandler = [NSURLConnection sendSynchronousRequest:request returningResponse:&requestResponse error:nil]; 
    NSError *error; 
    NSMutableDictionary *getJsonData = [NSJSONSerialization 
             JSONObjectWithData:requestHandler 
             options:NSJSONReadingMutableContainers 
             error:&error]; 

    if(error) 
    { 
     NSLog(@"%@", [error localizedDescription]); 
    } 
    else { 
     NSArray *json = getJsonData[@"data"]; 



     for (NSDictionary *jsn in json) 
     { 


      NSLog(@"dea %@",jsn[@"content"]); 
     } 





    } 

我的JSON的代碼是這樣

{"status":1,"error_message":null,"data":{"name":"Test","img":"","content":"Continut de test"}} 

我得到的錯誤是在「數據」鍵

[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x17e7b7c0 
2015-08-03 14:58:50.706 InfoCons[4418:1720073] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKeyedSubscript:]: unrecognized selector sent to instance 0x17e7b7c0' 
+0

正是在這行你得到的錯誤? –

回答

0

你在字典中甲數據JSON甲回報字典不陣列甲回報。

試試這個代碼得到JSON數據:

if(error) 
    { 
     NSLog(@"%@", [error localizedDescription]); 
    } 
    else { 
     NSLog(@"%@",getJsonData[@"data"][@"content"]); // print value of dictionary 
    }