JSONparsing在iPhone SDK

我有一个特定的JSON:

[ { "id" : 42422422, "created" : 1329684013, "name" : "Test" }, { "id" : 42422423, "created" : 1329684015, "name" : "Test 123" }, { ... } ] 

parsing这个确定,但是当networking服务器发生错误时,返回这个JSON:

 { "error" : { "code" : "511", "message" : "JSON error", "extra" : { "some" : "text", "someextra" : "text" } } } 

我试过使用这个:

 if ([jsonArray valueForKey:@"error"] != nil) { 

但是这是行不通的,因为如果我输出的值是'null'

我怎样才能检查这个? (我知道我可以使用NSRange ,但是我认为必须有更好的方法吗?

我parsing这样的JSON:

 NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error: &e]; 

responseData来自NSURLConnection的connectionDidFinishLoading方法。

你应该先检查你的结果是一个数组还是字典。

 if ([jsonArray isKindOfClass:[NSArray class]]) { //process results } else if ([jsonArray isKindOfClass:[NSDictionary class]]) { NSDictionary *errorInfo = [(NSDictionary *)jsonArray objectForKey:@"error"]; //handle error... } 

检查下面的链接,并简单地parsingJson。

获取Nextbelt的演示

下载JSON框架

检查此链接的JSONparsing

  NSString *appurl =[NSString stringWithFormat:@"your link"]; appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:appurl]]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse: nil error: nil ]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; NSMutableDictionary *deletdict=[returnString JSONValue]; if([[deletdict objectForKey:@"success"] isEqualToString:@"False"]) { NSLog(@"Unsuccess..."); } else { NSLog(@"success..."); } 

和Post方法是这样的

 NSString *post =[NSString stringWithFormat:@"AgencyId=STM&UserId=1&Type=1&Date=%@&Time=%@&Coords=%@&Image=h32979`7~U@)01123737373773&SeverityLevel=2",strDateLocal,strDateTime,dict]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://google/places"]]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSError *error; NSURLResponse *response; NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; NSMutableArray *SubLiveArray=[str JSONValue]; 

这是所有types的演示链接

有了您的无错误的JSON负载,您有一个顶级的数组(由开头[注意到]),而您的错误JSON负载则不。

 { "error" : { "code" : "511", "message" : "JSON error", "extra" : { "some" : "text", "someextra" : "text" } } } 

错误代码是一个嵌套的字典对象,因此您可以使用以下来parsing它:

 NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; NSString *errorCode = [NSString stringWithFormat:@"%@",[(NSDictionary*)[json objectForKey:@"error"]objectForKey:@"code"]]; 
 -(void)SerchpageApicall { NSString *main_url=@"yourlink"; NSString *appurl=[NSString stringWithFormat:@"%@&address=%@",main_url,_txt_Search_address.text]; appurl = [appurl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *url=[NSURL URLWithString:appurl]; NSURLRequest* requestVersion = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:5.0]; NSHTTPURLResponse* response = nil; NSError* error = nil; NSData *returnData = [NSURLConnection sendSynchronousRequest:requestVersion returningResponse:&response error:&error ]; NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding]; NSMutableDictionary *dict=[returnString JSONValue]; } 

使用[jsonArray objectForKey:@"error"]而不是valueForKey