javascript - node.js 将嵌套的 json 字符串转换为嵌套的 json 数组
问题描述
我正在使用 NODE.js MYSQL 为 REACT-NATIVE 应用程序编写 API,我正在使用两个表 posts[user_id, post_id, description, created_date] 和 files[post_id, saved_name],我加入这两个表并将 saved_name 列分组为文件名,
但是我对 mysql 查询结果有问题,它以 json 字符串格式出现,但我希望它是 JSON ARRAY 格式,一旦检查 put puts
#Current Out PUT
{
"status": 200,
"error": null,
"res_posts": [{
"post_id": 3,
"user_id": 1,
"description": " Working a Fine ",
"post_type": 0,
"created_date": "2019-01-25T18:40:41.000Z",
"saved_name": "8.jpg",
"file_Name": "7.jpg,8.jpg"
}, {
"post_id": 2,
"user_id": 1,
"description": " Hello hi",
"post_type": 1,
"created_date": "2019-01-21T12:51:16.000Z",
"saved_name": "4.jpg",
"file_Name": "6.jpg,5.jpg,4.jpg"
}, {
"post_id": 1,
"user_id": 1,
"description": " Hi How are you ",
"post_type": 0,
"created_date": "2019-01-21T12:50:51.000Z",
"saved_name": "1.jpg",
"file_Name": "3.jpg,2.jpg,1.jpg"
}]
}
#Required OUT PUT:
{
"status": 200,
"error": null,
"res_posts": [{
"post_id": 3,
"user_id": 1,
"description": " Working a Fine ",
"post_type": 0,
"created_date": "2019-01-25T18:40:41.000Z",
"saved_name": "8.jpg",
"file_Name": ["7.jpg", "8.jpg"]
}, {
"post_id": 2,
"user_id": 1,
"description": " Hello hi",
"post_type": 1,
"created_date": "2019-01-21T12:51:16.000Z",
"saved_name": "4.jpg",
"file_Name": ["6.jpg","5.jpg","4.jpg"]
}, {
"post_id": 1,
"user_id": 1,
"description": " Hi How are you ",
"post_type": 0,
"created_date": "2019-01-21T12:50:51.000Z",
"saved_name": "1.jpg",
"file_Name": ["3.jpg","2.jpg","1.jpg"]
}]
}
MYSQL OUTPUT : "file_Name": "3.jpg,2.jpg,1.jpg"
My Required OUT PUT: "file_Name": ["3.jpg","2.jpg","1.jpg"]
``````````````````````````````
i had a problem with file_Name out put it comes in string format but i want it in array , So please anyone help me in this to get required output .
解决方案
你可以在结构上映射。
const outputJson = `{
"status": 200,
"error": null,
"res_posts": [{
"post_id": 3,
"user_id": 1,
"description": " Working a Fine ",
"post_type": 0,
"created_date": "2019-01-25T18:40:41.000Z",
"saved_name": "8.jpg",
"file_Name": "7.jpg,8.jpg"
}, {
"post_id": 2,
"user_id": 1,
"description": " Hello hi",
"post_type": 1,
"created_date": "2019-01-21T12:51:16.000Z",
"saved_name": "4.jpg",
"file_Name": "6.jpg,5.jpg,4.jpg"
}, {
"post_id": 1,
"user_id": 1,
"description": " Hi How are you ",
"post_type": 0,
"created_date": "2019-01-21T12:50:51.000Z",
"saved_name": "1.jpg",
"file_Name": "3.jpg,2.jpg,1.jpg"
}]
}`;
const parsedJson = JSON.parse(outputJson);
parsedJson.res_posts = parsedJson.res_posts.map(item => {
item.file_Name = item.file_Name.split(',');
return item;
});
document.getElementById('content').innerHTML = JSON.stringify(parsedJson);
console.log(parsedJson);
<pre id="content"></pre>
推荐阅读
- ruby-on-rails - 如何将 find_by 应用于数组数组的元素 - Rails
- java - 打开 API UI 3.0.0 在底部隐藏架构,而不从 API 端点文档中删除它们
- stata - 输出比结果窗口可以记住的更广泛,因此日志捕获忘记记录
- firebase-cloud-messaging - 配置有一个 Firebase 项目的服务器可以向不同项目中应用的单个用户发送通知吗?
- c++ - 在 C++ 中读取非文本文件
- python - 将字典列表转换为 DataFrame 时出现 TypeError
- r - 将一列中以冒号分隔的字符串拆分为 R 中的不同列
- griddb - 开发人员机器不支持多播
- html - CSS 未在 springboot 模板 html 文件中应用
- css - 溢出-y自动后表格下方的空白