2014-04-10 12:06
浏览 78

无法在iOS中解析来自PHP的JSON get

I'm developing an app that needs to send request to web service and obtain JSON for the next process. I'm using AFNetworking for the request. Following is my PHP code

$data = array(
                'userID' => 'xxx'
echo json_encode($data);

I've already verified the above code, and the result printed is fine.


Then, I use AFNetworking to send a request for this Json data as below

NSDictionary *parameters = @{@"action": @"verifyUDID", @"UDID": udid};

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];

[manager POST:@"http://roommateradar.com/RoommateRadarAPI.php"  
         parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {

             NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);

The NSLog is correctly show the JSON data, but the next line which is parsing JSON to NSDictionary will cause a crash, the output is like below

2014-04-10 21:59:56.893 RoommateRadar[2941:60b] (
        userID = xxx;
2014-04-10 21:59:56.893 RoommateRadar[2941:60b] -[__NSCFArray bytes]: unrecognized selector sent to instance 0x1493e660
2014-04-10 21:59:56.895 RoommateRadar[2941:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray bytes]: unrecognized selector sent to instance 0x1493e660'

So you can see that the NSLog(@"%@",responseObject); is actually showing correct thing. BTW, I also tried

[NSJSONSerialization isValidJSONObject:responseObject];

to verify the responseObject, the result is also valid.

Any little help?

图片转代码服务由CSDN问答提供 功能建议

我正在开发一个需要向Web服务发送请求并获取下一个进程的JSON的应用程序。 我正在使用 AFNetworking 来处理请求。 以下是我的PHP代码

  $ data = array(
echo json_encode(  $ data); 



然后,我使用 AFNetworking 发送对此Json数据的请求, 下面

  NSDictionary * parameters = @ {@“action”:@“verifyUDID”,@“UDID”:udid}; 
AFAFTPRequestOperationManager * manager = [[AFHTTPRequestOperationManager alloc  ] init]; 
参数:参数成功:^(AFHTTPRequestOperation *操作,id responseObject){
 NSLog(@  “%@”,responseObject); 
 NSDictionary * dictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil]; 
}失败:^(AFHTTPRequestOperation * operation,NSError * er  ror){

NSLog 是 正确显示 JSON 数据,但是解析 JSON 到NSDictionary的下一行会导致崩溃,输出如下所示

  2014-04-10 21:59:56.893 RoommateRadar [2941:60b](
 userID = xxx; 
2014-04-10 21:59:56.893 RoommateRadar [2941  :60b]  -  [__ NSCFArray bytes]:无法识别的选择器发送到实例0x1493e660 
2014-04-10 21:59:56.895 RoommateRadar [2941:60b] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' -  [  __NSCFArray bytes]:无法识别的选择器发送到实例0x1493e660'

所以你可以看到NSLog(@“%@”,responseObject); 实际上是显示正确的事情。 顺便说一句,我也试过

  [NSJSONSerialization isValidJSONObject:responseObject]; 

来验证responseObject,结果 也有效。


  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhonglong3789 2014-04-10 12:10

    AFNetworking already deserialises the responseObject for you, so you don't need to use NSJSONSerialization.

    The crash is because you are passing the deserialised NSArray to NSJSONSerialization when it expects NSData.

    Also, when you do:

    NSDictionary *dictionary = [NSJSONSerialization ...

    you would not get a dictionary anyway, you would get an array. You should just have:

    NSArray *users = (NSArray *)responseObject;
    NSDictionary *user = [users objectAtIndex:0];

    (obviously you need to check the users array count)

    Also, when you use isValidJSONObject:, you're using it the wrong way round. It verifies if an object can be converted into JSON, not if the object can be converted from JSON.

    打赏 评论

相关推荐 更多相似问题