[英]json_decode multidimensional from database
我將此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"
}
}
然后,我從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。
它將其編碼為字符串,因為它是字符串。 您需要做的是首先對其進行解碼。
$response['price'] = json_decode($response['price']);
$json = json_encode($response,JSON_PRETTY_PRINT);
echo $json;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.