【问题标题】:JSON encoded dictionary in a dictionary not correctly deserialised using AFNetworking使用 AFNetworking 未正确反序列化字典中的 JSON 编码字典
【发布时间】:2015-10-13 18:49:04
【问题描述】:

在我的 PHP 后端,我返回一个 JSON,如下所示:

            $results_array=array();
            $stmt->bind_result($IdUser,$username);
            while($stmt->fetch()) {
                $row = array();
                $row["IdUser"]=$IdUser;
                $row["username"]=$username;
                $results_array[]=$row;

            }

            $stmt->close();
            echo json_encode(array("Response"=>"Login okay", "code"=>200, "results"=>$results_array));

我的 AFHTTPRequestOperation 管理器返回 responseObject(命名结果)。

现在我要获取用户:

NSDictionary *user = (NSDictionary *)[results objectForKey:@"results"];
NSLog(@"%@",user);
[[API sharedInstance] setUser:user];

[self.navigationController popViewControllerAnimated:YES];
[self showAlertWithTitle:@"Logged in" andMessage:[NSString stringWithFormat:@"Welcome %@",[user objectForKey:@"username"]]];

应用程序因"NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance" 而崩溃。我正在使用 NSLog 检查“用户”对象,我得到:

{ IdUser = 49; 用户名=肯尼; }

仍然是 JSON 格式。我在这里做错了什么?为什么它不反序列化为 NSDictionary 中的 NSDictionary?

【问题讨论】:

    标签: php ios objective-c json afnetworking


    【解决方案1】:

    您有一个“用户”类型的项目数组,“结果”包含字典数组而不是简单的字典。试试这个代码:

    NSArray *userList = results[@"results"];
    NSDictionary *user = userList.firstObject; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多