我使用echo json_encode(array);
Array (
[0] => Array (
[id] => 85
[name] => yeyery
[area_xy] => {"type": "rectangle","coordinates": {"point1":"22.272219163607744:114.13580417633057","point2":"22.275753627993897:114.1413402557373","point3":"22.27297371968214:114.14400100708008","point4":"22.270868895211578:114.13910865783691"}}
)
)
这是我得到的输出:
[
{
"id":"85",
"name":"yeyery",
"area_xy":"{\"type\": \"rectangle\",\"coordinates\": {\"point1\":\"22.272219163607744:114.13580417633057\",\"point2\":\"22.275753627993897:114.1413402557373\",\"point3\":\"22.27297371968214:114.14400100708008\",\"point4\":\"22.270868895211578:114.13910865783691\"}}",
}
]
但预期的输出是:
[
{
"id":"85",
"name":"yeyery",
"area_xy":{"type": "rectangle","coordinates": {"point1":"22.272219163607744:114.13580417633057","point2":"22.275753627993897:114.1413402557373","point3":"22.27297371968214:114.14400100708008","point4":"22.270868895211578:114.13910865783691"}},
}
]
在"area_xy"
之后我不想使用双引号来包装我的嵌套JSON。
答案 0 :(得分:5)
初始输出具有误导性;您应该使用var_export
,并且您会看到area_xy
值已经是字符串:
array (
0 =>
array (
'id' => 85,
'name' => 'yeyery',
'area_xy' => '{"type": "rectangle","coordinates": {"point1":"22.272219163607744:114.13580417633057","point2":"22.275753627993897:114.1413402557373","point3":"22.27297371968214:114.14400100708008","point4":"22.270868895211578:114.13910865783691"}}',
),
)
要先纠正它,decode,然后对整个shebang进行编码:
$arr[0]['area_xy'] = json_decode($arr[0]['area_xy']);
echo json_encode($arr, JSON_PRETTY_PRINT);