首页 > 解决方案 > 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 .

标签: javascriptmysqlnode.js

解决方案


你可以在结构上映射。

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>


推荐阅读