简体   繁体   English

节点:如何重组获取的嵌套JSON数据并组织可重用的数据?

[英]Node: How to restructure nested JSON data getting and organize reusable?

Using a Node request module, I am getting JSON data through an URL. 使用Node请求模块,我通过URL获取JSON数据。 The JSON file is nested in multiple levels and not organized in a way you can reuse. JSON文件嵌套在多个级别中,并且没有以可重复使用的方式进行组织。 Current JSON looks like this: 当前的JSON如下所示:

{
  "data": {
    "series": {
      "brochure_image": "range.jpg",
      "background_image": "range.jpg",
      "Model 1": {
        "medium_images": "Model_1.png",
        "brand": "Tesla",
        "Model 101": {
          "medium_images": "Model_101.png",
          "brochure_image": "Model_101.png",
        },
        "Model 102": {
          "medium_images": "Model_102.png",
          "brochure_image": "Model_102.png",
        }
      },
      "Model 2": {
        "medium_images": "Model_2.png",
        "brand": "Tesla",
        "Model 201": {
          "medium_images": "Model_201.png",
          "brochure_image": "Model_201.png",
        }
      }
    }
  }
}

I want to restructure so I can easily access the data from the front end. 我想进行重组,以便可以轻松地从前端访问数据。 I want to change like this: 我想这样改变:

[
  {
    "series":"Model 1",
    "model": [
      {
        "modelName": "Model 101",
        "medium_images": "Model_101.png",
        "brochure_image": "Model_101.png"
      },
      {
        "modelName": "Model 102",
        "medium_images": "Model_102.png",
        "brochure_image": "Model_102.png"
      }
    ] 
  }
  {
    "series":"Model 2",
    "model": [
      {
        "modelName": "Model 201",
        "medium_images": "Model_101.png",
        "brochure_image": "Model_101.png"
      }
    ] 
  }
]

How can I do this? 我怎样才能做到这一点? My node server.js getting part looks like this: 我的节点server.js参与如下:

const dataUrl = "http://example.com/data/series-data.json";

// API
app.get("/api/getSeriesData", (req, res) => {
    request.get(dataUrl, (error, response, body) => {
        var json = JSON.parse(body);
        var seriesObj = json.data.series;
        var seriesArr = [];

        for (var series in seriesObj) {

            if (seriesObj.hasOwnProperty(series)) {
                seriesArr.push({"series": series, "model": seriesObj[series]});
            }
        }
        res.send(seriesArr);
    });
});

You can do something like this: 您可以执行以下操作:

 var obj = { "data": { "series": { "brochure_image": "range.jpg", "background_image": "range.jpg", "Model 1": { "medium_images": "Model_1.png", "brand": "Tesla", "Model 101": { "medium_images": "Model_101.png", "brochure_image": "Model_101.png", }, "Model 102": { "medium_images": "Model_102.png", "brochure_image": "Model_102.png", } }, "Model 2": { "medium_images": "Model_2.png", "brand": "Tesla", "Model 201": { "medium_images": "Model_201.png", "brochure_image": "Model_201.png", } } } } }; var result = []; Object.keys(obj.data.series).forEach(key => { if(key.indexOf("Model") === 0){ var o = {series: key, model:[]}; Object.keys(obj.data.series[key]).forEach(k => { if(k.indexOf("Model") === 0){ o.model.push({modelName: k, medium_image: obj.data.series[key][k].medium_image, brochure_image: obj.data.series[key][k].brochure_image}); } }); result.push(o); } }); console.log(result); 

What this does is to loop through all the properties of the .data.series and data.series[MODEL NAME] and only consider those values of which property name starts with Model . 这样做是循环遍历.data.seriesdata.series[MODEL NAME]所有属性,并且仅考虑那些属性名称以Model开头的值。 If not all the values that you're interested in have their property name start with Model , this approach won't work. 如果并非所有您感兴趣的值的属性名称都以Model开头,则此方法将行不通。 In those cases, you'll probably have to use a negative assertion (check if their property name isn't one of the known ones you're not interested in). 在这些情况下,您可能必须使用否定断言(检查其属性名称是否不是您不感兴趣的已知名称之一)。

Here is my solution. 这是我的解决方案。

We can remove the unwanted keys in the object then loop through the object properties and add them to the array. 我们可以删除对象中不需要的keys ,然后遍历对象属性并将其添加到数组中。

Please go through the snippet and let me know if you face any issues or doubts. 请仔细阅读摘要,如果您遇到任何问题或疑问,请告诉我。

 var data = { "data": { "series": { "brochure_image": "range.jpg", "background_image": "range.jpg", "Model 1": { "medium_images": "Model_1.png", "brand": "Tesla", "Model 101": { "medium_images": "Model_101.png", "brochure_image": "Model_101.png", }, "Model 102": { "medium_images": "Model_102.png", "brochure_image": "Model_102.png", } }, "Model 2": { "medium_images": "Model_2.png", "brand": "Tesla", "Model 201": { "medium_images": "Model_201.png", "brochure_image": "Model_201.png", } } } } }; var output = []; delete data.data.series.brochure_image delete data.data.series.background_image for (k in data.data.series) { var temp = []; delete data.data.series[k].medium_images; delete data.data.series[k].brand; for (j in data.data.series[k]) { temp.push({ modelName: j, medium_images: data.data.series[k][j].medium_images, brochure_image: data.data.series[k][j].brochure_image }); } output.push({ "series": k, model: temp }); } console.log(output); 
  .as-console { height: 100%; } .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM