我将这个json保存在我的数据库中名为'price'的列中,其类型为'text'。
{
"desks": {
"Dedicated Desk": "$400 mo"
},
"private offices": {
"1 Person": "$550 mo",
"2 Person": "$1100 mo",
"3 Person": "$1600 mo",
"4 Person": "$2100 mo",
"6 Person": "$3300 mo"
},
"flexible membership": {
"Starting at": "$45 mo",
"Every Extra Day": "$50 Day"
}
}
然后我从PHP调用返回我数据库中的所有字段并将它们编码为json。
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;
$ response是数据库的响应。当我对$ response进行var_dump时,我得到了
array(22) {
[0]=>
object(stdClass)#6 (38) {
[...]
["price"]=>
string(242) "{"desks":{"Dedicated Desk":"$400 mo"},"private offices":{"1 Person":"$550 mo","2 Person":"$1100 mo","3 Person":"$1600 mo","4 Person":"$2100 mo","6 Person":"$3300 mo"},"flexible membership":{"Starting at":"$45 mo","Every Extra Day":"$50 Day"}}"
[...]
}
[...]
}
当我从json_encode回显结果时,我得到了
[
{
[...]
"price": "{\"desks\":{\"Dedicated Desk\":\"$400 mo\"},\"private offices\":{\"1 Person\":\"$550 mo\",\"2 Person\":\"$1100 mo\",\"3 Person\":\"$1600 mo\",\"4 Person\":\"$2100 mo\",\"6 Person\":\"$3300 mo\"},\"flexible membership\":{\"Starting at\":\"$45 mo\",\"Every Extra Day\":\"$50 Day\"}}",
[...]
},
[...]
]
我的问题是json_encode从数据库中获取json并将其格式化为字符串。我试图让它将其格式化为多维json结构的一部分。这就是我想要实现的目标:
[
{
"price":[
{
"desks":{
"Dedicated Desk":"$400 mo"
},
"private offices":{
"1 Person":"$550 mo",
"2 Person":"$1100 mo",
"3 Person":"$1600 mo",
"4 Person":"$2100 mo",
"6 Person":"$3300 mo"
},
"flexible membership":{
"Starting at":"$45 mo",
"Every Extra Day":"$50 Day"
}
}
]
}
]
任何帮助将不胜感激。运行最新版本的php。
答案 0 :(得分:0)
它将其编码为字符串,因为它是一个字符串。你需要做的是先解码它。
$response['price'] = json_decode($response['price']);
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;