【发布时间】:2020-03-12 12:35:51
【问题描述】:
问题是数组颜色中带引号的反斜杠。我认为这是因为 JSON_ARRAYAGG 但我不知道如何打印正确的 json。
查询:
SELECT a.id_product, JSON_ARRAYAGG(c.name_color) as colors, a.url
FROM products as a
LEFT JOIN product_has_colors b ON a.id_product = b.id_product
LEFT JOIN colors c ON c.id_color = b.id_color
GROUP BY a.id_product;
+------------+-------------------+-----------------+
| id_product | colors | url |
|------------+-------------------+-----------------+
| 1 | ["yellow", "blue"]| https://url.com |
| 2 | ["black, "green"] | https://url.com |
+------------+-------------------+-----------------+
PHP:
header('Content-Type: application/json);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
输出:
[
{
"id_product: "1",
"colors": [\"yellow\", \"blue\"]",
"url": "https://url.com"
},
{
"id_product: "2",
"colors": [\"black\", \"green\"]",
"url": "https://url.com"
}
]
【问题讨论】:
-
您的“输出”不是有效的 JSON 字符串。你是手写的吗?
标签: php mysql json mysql-json