在Javascript中从JSON输出数组对象

时间:2017-06-23 09:37:17

标签: javascript arrays json

JSON

{
    "path": "src/main/java/tools/generator/data/RecordPart.java",
    "revisions": 1,
    "codeLines": 18,
    "authors": [
      {
        "name": "tester",
        "commits": 1
      }
      {
        "name": "tester2",
        "commits": 6
      }
    ]
  },
...

想在Javascript中找到这个JSON:

我的代码:

var jsonSTRING= "[{\"path\": \"src/main/java/tools/generator/data/RecordPart.java\",\"revisions\": 25,\"codeLines\": 18,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]},{\"path\": \"src/main/java/tools/generator/data/RecordTotal.java\", \"revisions\": 55,\"codeLines\": 23,\"authors\": [{\"name\": \"User1\",\"commits\": 5}]},{\"path\": \"src/main/resources/test.xml\",\"revisions\": 102,\"codeLines\": 44,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]}]";  

var json = JSON.parse(jsonSTRING);

for(var i=0;i < json.length ; i++)
{
console.debug( json[i]["path"] );
console.debug( json[i]["revisions"] );
console.debug( json[i]["codeLines"] );
console.debug( json[i]["authors"] );
}

问题是,如果我得到这个输出是:

src/main/java/tools/generator/data/RecordPart.java  
25  
18  
[ Object ] [ Object ]

如何输出作者namecommits

4 个答案:

答案 0 :(得分:1)

var jsonSTRING= "[{\"path\": \"src/main/java/tools/generator/data/RecordPart.java\",\"revisions\": 25,\"codeLines\": 18,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]},{\"path\": \"src/main/java/tools/generator/data/RecordTotal.java\", \"revisions\": 55,\"codeLines\": 23,\"authors\": [{\"name\": \"User1\",\"commits\": 5}]},{\"path\": \"src/main/resources/test.xml\",\"revisions\": 102,\"codeLines\": 44,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]}]";  

var json = JSON.parse(jsonSTRING);

for(var i=0;i < json.length ; i++)
{
console.debug( json[i]["path"] );
console.debug( json[i]["revisions"] );
console.debug( json[i]["codeLines"] );
for(var j=0; j<json[i]["authors"].length; j++) {
    console.debug( json[i]["authors"][j].name );
    console.debug( json[i]["authors"][j].commits );
}
console.debug( json[i]["authors"] );
}

您也应该阅读数组。

答案 1 :(得分:0)

像这样:

var jsonSTRING= "[{\"path\": \"src/main/java/tools/generator/data/RecordPart.java\",\"revisions\": 25,\"codeLines\": 18,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]},{\"path\": \"src/main/java/tools/generator/data/RecordTotal.java\", \"revisions\": 55,\"codeLines\": 23,\"authors\": [{\"name\": \"User1\",\"commits\": 5}]},{\"path\": \"src/main/resources/test.xml\",\"revisions\": 102,\"codeLines\": 44,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]}]";  

var json = JSON.parse(jsonSTRING);

for(var i=0;i < json.length ; i++)
{
    console.debug( json[i]["path"] );
    console.debug( json[i]["revisions"] );
    console.debug( json[i]["codeLines"] );
    for(var k=0; k<json[i]["authors"].length; k++) {
        console.debug( json[i]["authors"][k].name );
        console.debug( json[i]["authors"][k].commits );
    }
}

答案 2 :(得分:0)

由于您的authors字段是一个数组,您可以尝试以下操作:

let authors = json.authors;


authors.forEach((author)=>{
    console.log(author.name);
    console.log(author.commits);
});

答案 3 :(得分:0)

您所要做的就是阅读作者数组

  var jsonSTRING= "[{\"path\": \"src/main/java/tools/generator/data/RecordPart.java\",\"revisions\": 25,\"codeLines\": 18,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]},{\"path\": \"src/main/java/tools/generator/data/RecordTotal.java\", \"revisions\": 55,\"codeLines\": 23,\"authors\": [{\"name\": \"User1\",\"commits\": 5}]},{\"path\": \"src/main/resources/test.xml\",\"revisions\": 102,\"codeLines\": 44,\"authors\": [{\"name\": \"User1\",\"commits\": 7}]}]";  

  var json = JSON.parse(jsonSTRING);

   json.filter(function(i){

       console.debug( i["path"] );
       console.debug( i["revisions"] );
       console.debug( i["codeLines"] );

      i["authors"].filter(function(o){
         console.debug( o["name"] );
         console.debug( o["commits"] );
   })
 });

我希望它有所帮助