josn转数组

$val = {“a”:true,”b”:”0″,”c”:”1″,”d”:”[{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”},{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”}]”,”success”:false,”message”:null}

转为array

if(is_array($val)) {
$val = array_map(‘unturn’, $val);
}else {
$val = stripslashes($val);
}
json_decode( $val, true);

出错 原因是 “ [ [ 外面还有个双引号 这个要怎么办啊?

回复讨论(解决方案)

$val = ‘{“a”:true,”b”:”0″,”c”:”1″,”d”:”[{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”},{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”}]”,”success”:false,”message”:null}’;
$a = json_decode($val, 1);
print_r($a);
$b = json_decode($a[‘d’], 1);
print_r($b);

array
(
[a] => 1
[b] => 0
[c] => 1
[d] => [{“e”:”1″,”f”:”123″,”g”:”321″,”h”:”234″,”date”:”2017-05-12 00:00:00″,”x”:”2″},{“e”:”1″,”f”:”123″,”g”:”321″,”h”:”234″,”date”:”2017-05-12 00:00:00″,”x”:”2″}]
[success] =>
[message] =>
)
array
(
[0] => array
(
[e] => 1
[f] => 123
[g] => 321
[h] => 234
[date] => 2017-05-12 00:00:00
[x] => 2
)
[1] => array
(
[e] => 1
[f] => 123
[g] => 321
[h] => 234
[date] => 2017-05-12 00:00:00
[x] => 2
)
)

这样吗?

$val = ‘{“a”:true,”b”:”0″,”c”:”1″,”d”:”[{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”},{\”e\”:\”1\”,\”f\”:\”123\”,\”g\”:\”321\”,\”h\”:\”234\”,\”date\”:\”2017-05-12 00:00:00\”,\”x\”:\”2\”}]”,”success”:false,”message”:null}’;
$data = json_decode($val, true);
$result = array();
foreach($data as $k=>$v){
if(json_decode($v)){
$result[$k] = json_decode($v, true);
}else{
$result[$k] = $v;
}
}
print_r($result);

array
(
[a] => 1
[b] => 0
[c] => 1
[d] => array
(
[0] => array
(
[e] => 1
[f] => 123
[g] => 321
[h] => 234
[date] => 2017-05-12 00:00:00
[x] => 2
)
[1] => array
(
[e] => 1
[f] => 123
[g] => 321
[h] => 234
[date] => 2017-05-12 00:00:00
[x] => 2
)
)
[success] =>
[message] =>
)

Posted in 未分类

发表评论