我正在使用SQL及其JSON数据类型,我已经插入了一个数组对象并追加了新对象,但是当我使用
"UPDATE introductions SET all_replies = COALESCE(JSON_ARRAY_APPEND(roles, '$', '"
+ JSON.stringify(appendedData)
+ "'), JSON_ARRAY(null)) WHERE id2 = 1;"
,然后在节点上解析数据时不起作用。 这是插入数据的图片,底部是附加数据
但是您可以看到底部的“ id-4”是不同的,当将其打印到我的终端设备时,我已经尝试对其进行解析了
console.log(JSON.parse(results[0].all_replies))
事实上,我已经尝试了多种方法
console.log(eval(JSON.parse(result[0].all_replies)))
console.log(eval(result[0].all_replies))
console.log(JSON.parse(JSON.stringify(result[0].all_replies)))
所以基本上,每当我追加一个新对象时,它都不会让我像插入它一样来解析它。 对不起,我事先的解释很糟糕。
var theJSON = [
{ "id": 1,
"info": {
"username": "amazon",
"message": "jeff bezox.",
"likes": 222,
"time_created": ers
}
},
{ "id": 2,
"info": {
"username": "facebook",
"message": "ik you bod",
"likes": 92,
"time_created": ers
}
},
{ "id": 3,
"info": {
"username": "fortnite",
"message": "kids love me lol",
"likes": 12,
"time_created": ers
}
}]
var appenedData = { "id": 1,
"info": {
"username": "amazon",
"message": "jeff bezox.",
"likes": 222,
"time_created": ers
}
}
答案 0 :(得分:0)
就MySQL而言,您正在向JSON数组添加一个字符串。
要让其将JSON解释为JSON,您需要进行强制转换。
例如
let query = `UPDATE introductions
SET all_replies = COALESCE(
JSON_ARRAY_APPEND(roles, '$', CAST('${JSON.stringify(appendedData)}' AS JSON)),
JSON_ARRAY(null)
) WHERE id2 = 1`
演示〜https://www.db-fiddle.com/f/xnTrecnFBgiQWwuvTaAYu4/0
请参见https://dev.mysql.com/doc/refman/8.0/en/json.html#json-converting-between-types
仅供参考,在回答之前,我对此主题一无所知,并在MySQL手册中找到了我所需的一切。这是一个很棒的资源,非常值得阅读。