插入Mysql表时防止Json排序 [英] Prevent Json ordering while inserting into Mysql table
问题描述
在发送 AJAX 请求时,Json 内容的重新排序已经是一个已知问题,但我不知道在将 Json 内容插入具有 JSON 类型的 Mysql 表时会发生同样的情况.
The reordering of Json-Content is already an known issue while sending AJAX Request, but I wasn't aware that the same happens while inserting Json-content into an Mysql Table with JSON type.
在这种情况下,mysql 服务器也会在保存前对其内容进行排序.
In this case the mysql server also orders its content before saving.
之前:
{"c":3, "b":2, "a":1}
之后:
{"a":1, "b":2, "c":3}
假设我想出于某种目的阻止这种排序......是否有可能在不调整 Json 数据本身的情况下禁用排序?
Suppose I would like to prevent this ordering for some purpose... Is there any possibility to disable the ordering without adapting the Json data itself?
推荐答案
这个的关键是把这个序列化 > 然后存储 > 检索 > 然后解析
The key to this is to serialize this > then store it > retrieve > then parse
例如:首先将对象存储为字符串
For example: First store object as a string
var jsonDataStringed = JSON.stringify(myJsonData);
INSERT INTO MY_TABLE(id,jsonDataStringed);
然后检索字符串并将其改回对象
then to retrieve the string and change it back to object
db.sequelize.query(`SELECT * FROM MY_TABLE`);
var myJsonData = JSON.parse(db[0])
这篇关于插入Mysql表时防止Json排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!