JSON 字典中的编码字典未使用 AFNetworking 正确反序列化

JSON encoded dictionary in a dictionary not correctly deserialised using AFNetworking

在我的 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 管理器 returns 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 检查 "user" 对象,我得到:

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

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

您有一个 "User" 类型的项目数组,"results" 包含字典数组而不是简单的字典。试试这个代码:

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