0

我有下一个 php 脚本:

<?PHP
$results = array(
    array(
        'col1'=>'val1.1',
        'col2'=>'val1.2'
    ),
    array(
        'col1'=>'val2.1',
        'col2'=>'val2.2'
    )
);
echo json_encode($results);

?>

主数组中的每个数组都代表数据库中的原始数据。结果变量被发送到 OSx 应用程序。我正在尝试解码 json 字符串以将本地数据库与远程数据库同步。

我正在使用这段代码:

NSError *error = nil;
id object = [NSJSONSerialization
                     JSONObjectWithData:receivedData
                     options:0
                     error:&error];

if(error) { /* JSON was malformed, act appropriately here */ }
if([object isKindOfClass:[NSDictionary class]]){
  NSDictionary *results = object;       
  NSLog(@"%@",results);
}else{
  NSLog(@"there is not an JSON object");
}

问题是应用程序无法将接收到的字符串识别为对象。有人可以解释我为什么以及我做错了什么吗?

收到的字符串是:

[{"col1":"val1.1","col2":"val1.2"},{"col1":"val2.1","col2":"val2.2"}]
4

0 回答 0