首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >json_encode (PHP) + JSON_ARRAYAGG (mySQL)中的反斜杠

json_encode (PHP) + JSON_ARRAYAGG (mySQL)中的反斜杠
EN

Stack Overflow用户
提问于 2019-11-16 16:02:20
回答 2查看 873关注 0票数 0

问题是带有数组颜色中引号的反斜杠。我认为这是因为JSON_ARRAYAGG,但我不知道如何打印正确的json。

查询:

代码语言:javascript
复制
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:

代码语言:javascript
复制
header('Content-Type: application/json);
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

产出:

代码语言:javascript
复制
[
  {
    "id_product: "1",
    "colors": [\"yellow\", \"blue\"]",
    "url": "https://url.com"
  },
 {
    "id_product: "2",
    "colors": [\"black\", \"green\"]",
    "url": "https://url.com"
  }
]
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-16 16:15:37

您正在对colors编码两次。首先在SQL中,然后在PHP中。您可以做的是在colors编码$data之前在中解码$data

代码语言:javascript
复制
foreach ($data as $key = $row) {
    $data[$key]['colors'] = json_decode($row['colors']);
}

如果您将获取作为对象,则使用以下命令:

代码语言:javascript
复制
foreach ($data as $obj) {
    $obj->colors = json_decode($obj->colors);
}

或者在SQL中完全生成JSON:

代码语言:javascript
复制
SELECT JSON_ARRAYAGG(JSON_OBJECT(
    'id_product', id_product,
    'colors', colors,
    'url', url
)) as json
FROM (
    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
) x
票数 0
EN

Stack Overflow用户

发布于 2021-04-13 20:02:19

我在MYSQL上使用JSON_ARRAYAGG,在PHP上使用这个函数来删除反斜杠:

代码语言:javascript
复制
function isJson($string) {
        json_decode($string, true);
        return (json_last_error() == JSON_ERROR_NONE);
    }
    
    
    function result_as_json($result, $array_columns = null){
        $json = array();
        foreach($result as $res) {
            $item = array();
            if($array_columns != null){
                foreach ( $array_columns as $key) {
                    $item[$key] = isJson($res[$key])?json_decode($res[$key], true):$res[$key];
                }
            }
            else {
                foreach ($res as $key => $value) {
                    $item[$key] = isJson($value)?json_decode($value, true):$value;
                }
            }
            array_push($json, $item);
        }
        return json_encode($json);
    }

我希望它能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58892577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档