我在Node.js中创建了一个快速应用程序
我使用MySQL作为文档存储
Columns
id: integer
value: JSON
当我通过mysql查询MySQL数据库时,我得到了结果:
RowDataPacket {
id: '1',
date: '2018-02-25T17:48:52.000Z',
value: '{"name": "Andreas", "username": "andreaslacza", "gender": "Male"}'
},
// ... and so on
你可以看到我的JSON嵌套在值下,但我需要一个输出如下,以便在胡子/把手中使用它:
RowDataPacket {
"id": 1,
"date": "2018-02-25T17:48:52.000Z",
"value": {
"name": "Andreas",
"username": "andreaslacza",
"gender": "Male"}
},
答案 0 :(得分:0)
您需要遍历结果数组并对每个值应用JSON.parse
。
喜欢这个
'use strict'
let resultData = [
{
id: '1',
date: '2018-02-25T17:48:52.000Z',
value: '{"name": "Andreas", "username": "andreaslacza", "gender": "Male"}'
},{
id: '1',
date: '2018-02-25T17:48:52.000Z',
value: '{"name": "Andreas", "username": "andreaslacza", "gender": "Male"}'
}
];
resultData.forEach(function(entry) {
entry.value = JSON.parse(entry.value);
});
console.log(resultData);