我有一个JSON响应,如下所示:
{"order_product_id":"6","design_order":["design_number_1"],"design_number_1":{"sort_order":["Design Number"],"Design Number":"1"}}
我试图把它变成像这样的PHP对象:
$obj = json_decode($data,true);
但是$ obj总是空的。谁能告诉我我做错了什么?
修改
这是我的ajax请求。
response = JSON.stringify(response);
$.ajax({
type: "POST",
url: "<?php echo $submit_url; ?>",
data: { 'data' :response},
success: function(data){
alert(data);
console.log(data);
}
})
答案 0 :(得分:3)
true
作为第二个参数(这意味着“创建一个关联数组而不是一个对象)这样:
<?php
$data = "{"order_product_id":"6","design_order":["design_number_1"],"design_number_1":{"sort_order":["Design Number"],"Design Number":"1"}}";
$data = htmlspecialchars_decode($data);
$obj = json_decode($data);
print_r($obj);
?>
输出:
stdClass Object
(
[order_product_id] => 6
[design_order] => Array
(
[0] => design_number_1
)
[design_number_1] => stdClass Object
(
[sort_order] => Array
(
[0] => Design Number
)
[Design Number] => 1
)
)