ds342222222 2014-04-10 12:06
浏览 89
已采纳

无法在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(
            array(
                'userID' => 'xxx'
            )
        );
echo json_encode($data);

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

[{"userID":"xxx"}]

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) {

             NSLog(@"%@",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?

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境