您的位置:首页 > 移动开发 > Objective-C

PHP json_decode($json, TRUE) TRUE使数据格式化为Array,而非object

2013-04-12 17:19 453 查看
PHP使用json_encode处理后为非标准json, 使用json_decode($json) 解析后会变成object类型

json_decode()有第二个参数可以来处理,设置第二个参数为TRUE json_decode($json, TRUE)

$array = array('name'=>'sean', 'age'=>12);

$j_arr = json_encode($array);

$j_arrs = json_decode($j_arr);//json_decode($json, TRUE)没有设置第二个参数时

echo '<pre>';
var_dump($j_arrs);
echo '</pre>';

结果为对象
object(stdClass)#1 (2) {
["name"]=>
string(4) "sean"
["age"]=>
int(12)
}

//设置json_decode() 第二个参数TRUE返回标准格式数据
$j_arrs = json_decode($j_arr, TRUE);

//设置为TRUE返回数组
array(2) {
["name"]=>
string(4) "sean"
["age"]=>
int(12)
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: