如何在nodejs中循环访问JSON数组?

时间:2016-12-09 00:52:02

标签: javascript json node.js node-request

我正在尝试从json数组读取值以显示在页面中。我尝试使用下面的代码,但无法实现。我想要很长时间才能完成这件事。请告诉我这里做错了什么。

此外,我无法执行JSON.parse-意外输入错误。

http.request(options, function(res) {
res.on('data', function (result) {
console.log(result);//Displaying below format of result without any error
console.log(result.Reference[0].name); Error //TypeError: Cannot read property '0' of undefined.
console.log(result.Reference.length);//Error TypeError: Cannot read property 'length' of undefined

JSON格式:打印结果时

{
        "Reference": [
            {
                "name": "xxxxxxxx",
                "typeReference": {
                 "articulation": 0,
                "locked": false,
                "createdBy": {
                     "userName": "System",
                              },
                "lastModifiedBy": {
                                  "userName": "System",
                                 },

                "lastModified": 1391084398660,
                "createdOn": 1391084398647,
                "isSystem": true
            },
            "communityReference": {
                "name": "xxxxxx",
                "language": "English",
                "sbvr": false,
                 "parentReference": {
                    "name": "xxxxx",
                    "sbvr": false,
                    "meta": false,
                    "parentReference": null,
                    "locked": false,
                    "createdBy": {
                        "userName": "xxxxx",
                           },
                    "lastModifiedBy": {
                        "userName": "xxxxx",
                           },

                    "lastModified": 1459185726230,
                    "createdOn": 1456337723119,
                    "isSystem": false
                },
                "locked": false,
                "createdBy": {
                     "userName": "xxxxx",
                           },
                "lastModifiedBy": {
                    "userName": "xxxxxx",
                          },

                "lastModified": 1472655031590,
                "createdOn": 1472654988012,
                "isSystem": false
            },
            "locked": false,
            "createdBy": {
                  "userName": "xxxxx",

            },
            "lastModifiedBy": {

                "userName": "xxxxx",
                "firstName": "xxxxx",

            },

            "lastModified": 1473171981520,
            "createdOn": 1472655253366,
            "isSystem": false
        },
        {
        "name":"yyyyyy", same attribute type as above.
          ...
        },
        {
        ..
        },

2 个答案:

答案 0 :(得分:2)

res是一个流,data事件表示它已收到一些数据,但它可能是也可能不是所有数据。您不可能一次获得所有数据,所以当您拥有整个JSON对象时,需要等到end事件触发:

var json = '';
res.on('data', function ( chunk ) {
  json += chunk;
} );
res.on('end', function ( ) {
  var result = JSON.parse( json );
  console.log( result.Reference[0].name );
} );

或者您可以使用json-stream,它可以通过块读取JSON块并正确处理,与JSON.parse不同。

但是,我建议您不要使用上述任一解决方案,而是使用request,这极大地简化了此请求以及处理HTTP请求和处理响应的其他方面。

答案 1 :(得分:-2)

您可以尝试使用其他一些安全的验证:

{
  if ( result ) {
    var data = (typeof result == "string") ? JSON.parse(result) : result;
    if (data.hasOwnProperty("Reference")) {
      for(var i = 0; i < data.Reference.length; i++; ) {
        try {
            var reference = data.Reference[i];
            console.log("Name:" + reference.name);
            //Your app logic
        } catch(e) {
            //In case if any invalid references, then continue with others
            console.warn("Error processing reference - " + e);
        }
      }
    } else {
      console.warn("No References were found!");
    }
  } else {
    console.error("Invalid JSON!");
  }
}

希望它有所帮助!

相关问题