用php呈现MYSQL嵌套JSON对象
我使用000webhostapp cpanel存储了一个MYSQL表,下面是它的样子用php呈现MYSQL嵌套JSON对象,php,json,http,mysqli,Php,Json,Http,Mysqli,我使用000webhostapp cpanel存储了一个MYSQL表,下面是它的样子 id | name | J_Object --------------------------------------------------------- 1 | deckA | {"type":"A", "ports": {"hdmi": 1, "usb":2}} 2 | deckB | {"type":"B", "ports": {"hdmi": 3, "usb":2}}
id | name | J_Object
---------------------------------------------------------
1 | deckA | {"type":"A", "ports": {"hdmi": 1, "usb":2}}
2 | deckB | {"type":"B", "ports": {"hdmi": 3, "usb":2}}
3 | deckC | {"type":"A", "ports": {"hdmi": 1, "usb":2}}
4 | deckD | {"type":"B", "ports": {"hdmi": 3, "usb":2}}
通过使用PHP,我想将其如下所示,作为HTTP将数据传递到其他平台
[
{
"id": "1",
"name": "deckA",
"J_Object": [{"type":"A", "ports": {"hdmi": 1, "usb":2}}]
},
{
"id": "2",
"name": "deckB",
"J_Object": [{"type":"B", "ports": {"hdmi": 3, "usb":2}}]
},
{
"id": "1",
"name": "deckC",
"J_Object": [{"type":"A", "ports": {"hdmi": 1, "usb":2}}]
},
{
"id": "1",
"name": "deckD",
"J_Object": [{"type":"B", "ports": {"hdmi": 3, "usb":2}}]
}
]
然而,在我下面编写的PHP中,后面的内容就是我得到的。这绝对不是我想要的
PHP代码:
$search = "SELECT * FROM `database`.`products`
WHERE JSON_EXTRACT(`J_Object` , '$.ports.usb') > 2
AND JSON_EXTRACT(`J_Object` , '$.ports.hdmi') > 1;";
$result = mysqli_query($link, $search);
if ($result) {
//Printing out the details of the rows
while($array = mysqli_fetch_assoc($result)) {
$jsonData[]=$array;
}
$nonArr = json_encode($jsonData, JSON_PRETTY_PRINT);
$stringArr = (string)$nonArr;
$nonSlash = str_replace("\\","", $stringArr);
echo $nonSlash;
}
mysqli_close($link)
以下是不必要的结果:
[
{
"id": "1",
"name": "deckA",
"J_Object": "{"type":"A", "ports": {"hdmi": 1, "usb":2}}"
},
{
"id": "2",
"name": "deckB",
"J_Object": "{"type":"B", "ports": {"hdmi": 3, "usb":2}}'
},
{
"id": "1",
"name": "deckC",
"J_Object": "{"type":"A", "ports": {"hdmi": 1, "usb":2}}"
},
{
"id": "1",
"name": "deckD",
"J_Object": "{"type":"B", "ports": {"hdmi": 3, "usb":2}}"
}
]
看起来是一样的,但是如果您注意到[]被“”更改,这也意味着嵌套对象不太正确。
任何专业人士都能告诉我如何获得正确的输出吗?这对你来说很有用。这只是一个快速修复preg_替换
$result = mysqli_query($link, $search);
if ($result) {
//Printing out the details of the rows
while($array = mysqli_fetch_assoc($result)) {
$jsonData[]=$array;
}
$nonArr = json_encode($jsonData, JSON_PRETTY_PRINT);
$stringArr = (string)$nonArr;
$nonSlash = str_replace("\\","", $stringArr);
// quick fix
$nonSlash = preg_replace("/[\"]{1}[{]{1}/", '[{', $nonSlash);
$nonSlash = preg_replace("/[}]{1}[\"]{1}/", '}]', $nonSlash);
echo $nonSlash;
}
输出:
[
{
"id": "1",
"name": "deckA",
"J_Object": [{"type":"A", "ports": {"hdmi": 1, "usb":2}}]
},
{
"id": "2",
"name": "deckB",
"J_Object": [{"type":"B", "ports": {"hdmi": 3, "usb":2}}]
},
{
"id": "3",
"name": "deckC",
"J_Object": [{"type":"A", "ports": {"hdmi": 1, "usb":2}}]
},
{
"id": "4",
"name": "deckD",
"J_Object": [{"type":"B", "ports": {"hdmi": 3, "usb":2}}]
}
]
希望这对你有所帮助。快乐编码..您可以尝试一种更简单的方法
$result = mysqli_query($link, $search);
if ($result) {
//Printing out the details of the rows
while($array = mysqli_fetch_assoc($result)) {
$array['J_Object'] = [json_decode($array['J_Object'], true)];
$jsonData[]=$array;
}
echo json_encode($jsonData, JSON_PRETTY_PRINT);
}
我不明白为什么要存储json,而您可以使用其他列(type、port_hdmi和port_usb)或其他表来存储这些数据。另外,我不明白为什么你想要结果作为一个数组,虽然它不是像这样存储的hi@Zyigh我存储为一个json,这样我可以有可伸缩性,他们节省了一些空间。我还编辑了一些,因为它们存储为数组。很好。多谢各位