如何循环遍历nodejs中的JSON数组?
How to loop through JSON array in nodejs?
我正在尝试从 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.
...
},
{
..
},
res
是一个流,当您拥有整个 JSON 对象时,data event indicates that it has received some data, but it may or may not be all the data. You're not likely to get all the data at once, so you need to wait until the 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 请求和处理响应的这一方面和其他方面。
您可以尝试进行一些额外的安全验证:
{
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!");
}
}
希望对您有所帮助!
我正在尝试从 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.
...
},
{
..
},
res
是一个流,当您拥有整个 JSON 对象时,data event indicates that it has received some data, but it may or may not be all the data. You're not likely to get all the data at once, so you need to wait until the 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 请求和处理响应的这一方面和其他方面。
您可以尝试进行一些额外的安全验证:
{
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!");
}
}
希望对您有所帮助!