【问题标题】:node.js convert nested json string to nested json arraynode.js 将嵌套的 json 字符串转换为嵌套的 json 数组
【发布时间】:2020-02-16 18:41:43
【问题描述】:

我正在使用 NODE.js MYSQL 为 REACT-NATIVE 应用程序编写 API, 我正在使用两个表 post[user_id, post_id, description, created_date] 和 files[post_id, saved_name],我加入这两个表并将 saved_name 列分组为 file_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 .

【问题讨论】:

  • 请分享查询
  • 没有什么能比得上json stringjson array 格式。 json 是基于字符串的数据表示,如果您想使用它,则必须对其进行解析。所以要么是json,要么不是。
  • 感谢您的重播,但我需要从当前输出到所需输出的解决方案

标签: javascript mysql node.js


【解决方案1】:

你可以在结构上映射。

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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    相关资源
    最近更新 更多