简体   繁体   English

如何使复杂的Json适合Javascript对象

[英]How to make complex Json fit a Javascript object

The backend of my webapp, written in node.js interacts with Json file, with a specific format that I thought not so complex but apparently is. 用node.js编写的webapp的后端与Json文件交互,具有我认为不那么复杂但显然是特定格式的特定格式。

The structure of my json file is as such : 我的json文件的结构是这样的:

{
    "data": [
      {
        "somefield": "ioremipsum",
        "somedate" : "2018-08-23T11:48:00Z",
        "someotherdate" : "2018-08-23T13:43:00Z",
        "somethingelse":"ioremipsum",
        "files": [
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          },
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          },
          {
            "specificfieldinarray": "ioremipsum",
            "specificotherfieldinarray": "ioremipsum"
          }
        ]
      }
    ]
  }

I try to make this answer fit a JS object like this : 我试着让这个答案适合像这样的JS对象:


const file =  require('specificJsonFile.json');

let fileList = file;

And I need to loop through my 'files' array, for further treatments, but unfortunately, my JS object looks like this : 我需要遍历我的'files'数组,以进行进一步的处理,但不幸的是,我的JS对象看起来像这样:


{ data:
   [ { somefield: "ioremipsum",
       somedate : "2018-08-23T11:48:00Z",
       someotherdate : "2018-08-23T13:43:00Z",
       somethingelse:"ioremipsum",
       files: [Array] } ] }

Please forgive me if this is obvious, for I am still a beginner with JS. 如果这很明显,请原谅我,因为我还是JS的初学者。

That's only how console.log logs deep objects. 这只是console.log记录深层对象的方式。 To get a deeper output, you can use util.inspect 要获得更深入的输出,可以使用util.inspect

const util = require('util');
console.log(util.inspect(yourObject, {showHidden: false, depth: null}));

To loop each data's files, simply loop data, then its files 要循环每个数据的文件,只需循环数据,然后循环其文件

yourObject.data.forEach(d => {
    d.files.forEach(file => console.log(file));
});

It looks like there is nothing wrong there and the console is abbreviating the log. 看起来那里没有任何错误,控制台正在缩写日志。

Try accessing the files list with the following code: 尝试使用以下代码访问文件列表:

const filesList = file.data[0].files

and then 接着

console.log(filesList) to check that it's eventually working. console.log(filesList)来检查它是否最终正常工作。

Hope it helps! 希望能帮助到你!

let fileList = file.data[0].files;

This will create an array of only your files array. 这将创建仅包含文件数组的数组。

You can console.log(fileList) 你可以console.log(fileList)

Or whatever you like with the data. 或者你喜欢的任何数据。

Based on your comment, try the of keyword instead of in keyword to get the behaviour you expected. 根据您的评论,尝试使用of keyword而不是in keyword来获取您期望的行为。

for (let file of fileList){
    console.log(file);
}

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of

You can use for in 您可以使用for in

for (item in fileList.data) {
    for (file in fileList.data[item].files) {
         let data = fileList.data[item].files[file];

         // process the data         

    }
}

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

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