iOS的JSONparsing不工作(返回空字典)

我使用NSJSONSerializationJSONObjectWithData:data options: error:parsing从服务器返回的JSON数据。

现在我使用的选项参数: NSJSONReadingAllowFragments 。 你可以看下面,看看实际的JSON(我相信问题是)。

我得到的错误消息是:

错误域= NSCocoaErrorDomain代码= 3840“操作无法完成(cocoa错误3840.)”(无效的值在字符0周围)。UserInfo = 0x6895da0 {NSDebugDescription =字符0周围的无效值。}

任何想法如何解决它?

JSON =

 {"name":"Johan Appleseed", "email":"j.appleseed@emuze.co", "phone":"+4121876003", "accounts":{ "facebook":[true,1125], "twitter":[false,null], "homepage":[true,"http:\/\/johnAplleseed.com\/index.html"]}} 

也许你有一些你无法看到的不可打印的字符。 尝试这个:

 NSData *jsonData = ... const unsigned char *ptr = [data bytes]; for(int i=0; i<[data length]; ++i) { unsigned char c = *ptr++; NSLog(@"char=%c hex=%x", c, c); } 

要validation您在数据的开头或末尾没有不可打印的字符。

编辑:澄清,只是在您的JSON字典上运行以上 – 无法parsing的。

我发现这个问题是因为URL的返回是一个HTML页面,所以这些html,head和body标签都围绕着实际的响应,所以不能被parsing。 这是一个很好的问答,关于如何从响应中删除HTML标签(在它被更改为string之后): 从iPhone上的NSString中删除HTML标签

我有同样的问题一段时间,我只知道,如果我从网页拉数据让我们说PHP页面,不应该在该页面中的任何HTML标记。 所以像这样的结构:

 <html> <body> <?php ?> </body> </html> 

会毁了你的结果。 把它变成:

 <?php ?> 

为我工作。

已经有一段时间了,但打印数据的一个更简单的方法是:

NSLog(@“%@”,[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

一种方法是,你可以通过post请求parsingjsondata

  -(void)callWebserviceList { spinner.hidden=NO; NSString *bodyData = @"code_request=monuments_list&asi_id=1"; NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://asicircles.com/server_sync.php"]]; // Set the request's content type to application/x-www-form-urlencoded [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; // Designate the request a POST request and specify its body data [postRequest setHTTPMethod:@"POST"]; [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]]; 

connection1 = [NSURLConnection connectionWithRequest:postRequest delegate:self];

 if(connection1 !=nil) { ////NSLog(@"%@",postRequest); } 

}

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { if ([connection isEqual:connection1 ]) { [responseData setLength:0]; }else if ([connection isEqual:connection2 ]) { [responseData1 setLength:0]; } 

} – (void)连接:(NSURLConnection *)连接didReceiveData:(NSData *)data {

  if ([connection isEqual:connection1 ]) { [responseData appendData:data]; }else if ([connection isEqual:connection2 ]) { [responseData1 appendData:data]; } //**check here for responseData & also data** 

}

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]); } - (void)connectionDidFinishLoading:(NSURLConnection *)connection { if ([connection isEqual:connection1 ]) { spinner.hidden=YES; NSError *error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; NSMutableArray *arrdata=[json objectForKey:@"message"]; NSLog(@"code is%@", json); for (int i=0; i< arrdata.count; i++) { [arrDetails addObject:[[arrdata objectAtIndex:i]objectForKey:@"details"]]; [arrImageUrl addObject:[[arrdata objectAtIndex:i]objectForKey:@"image"]]; [arrLat addObject:[[arrdata objectAtIndex:i]objectForKey:@"lat"]]; [arrLongi addObject:[[arrdata objectAtIndex:i]objectForKey:@"longi"]]; [arrName addObject:[[arrdata objectAtIndex:i]objectForKey:@"name"]]; [arrLoc addObject:[[arrdata objectAtIndex:i]objectForKey:@"location"]]; [arrID addObject:[[arrdata objectAtIndex:i]objectForKey:@"id"]]; NSLog(@"code is%@",[[arrdata objectAtIndex:i]objectForKey:@"details"]); NSLog(@"code is%@",[arrImageUrl objectAtIndex:i]); } if (arrName.count > 0) { [self addscrollView]; } }else if ([connection isEqual:connection2 ]) { } } 

你也可以在ios中按块打json数据的url

  #define kBgQueue dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) //1 #define kLatestKivaLoansURL [NSURL URLWithString: @"http://api.kivaws.org/v1/loans/search.json?status=fundraising"] //2 

我们需要做的第一件事是从网上下载JSON数据。 幸运的是,通过GCD,我们可以在一行代码中完成这个工作! 将以下内容添加到ViewController.m中:

 - (void)viewDidLoad { [super viewDidLoad]; dispatch_async(kBgQueue, ^{ NSData* data = [NSData dataWithContentsOfURL: kLatestKivaLoansURL]; [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES]; }); } - (void)fetchedData:(NSData *)responseData { //parse out the json data NSError* error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData //1 options:kNilOptions error:&error]; NSArray* latestLoans = [json objectForKey:@"loans"]; //2 NSLog(@"loans: %@", latestLoans); //3 }