[英]json_decode multidimensional from database
I have this json saved in my database in a column called 'price' with the type of 'text'. 我将此json保存在数据库中名为“价格”且类型为“文本”的列中。
{
"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"
}
}
I then make a call from PHP to return my all the fields in my database and encode them as json. 然后,我从PHP进行调用以返回数据库中的所有字段并将它们编码为json。
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;
$response is the response from the database. $ response是来自数据库的响应。 When I var_dump on $response I get 当我在$ 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"}}"
[...]
}
[...]
}
When I echo the result from json_encode i get 当我从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\"}}",
[...]
},
[...]
]
My problem is json_encode is taking the json from the database and formatting it as a string. 我的问题是json_encode从数据库获取json并将其格式化为字符串。 I am trying to get it to format it as part of a multidimensional json structure. 我正在尝试将其格式化为多维json结构的一部分。 This is what im trying to achieve: 这是我试图实现的目标:
[
{
"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"
}
}
]
}
]
Any help would be appreciated. 任何帮助,将不胜感激。 Running latest version of php. 运行最新版本的php。
It is encoding it as a string because it IS a string. 它将其编码为字符串,因为它是字符串。 What you need to do is decode it first. 您需要做的是首先对其进行解码。
$response['price'] = json_decode($response['price']);
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.