gpt4 book ai didi

javascript - 使用node.js解析json时遇到问题

转载 作者:行者123 更新时间:2023-11-28 19:54:24 25 4
gpt4 key购买 nike

我编写了一个快速脚本来解析两个相当大的 json 文件(约 17k 条记录),以对两者进行比较。我已经确认它们都是有效的 json(通过 jsonlintpro)并且格式相同。 (来源是相同的,所以这应该是给定的。但是,我总是认为错误是我的。而且我仍然这样做。只是在其他地方。)但是,解析的文件只输出 [object, Object]。我想知道可能是什么原因?

json 格式就像这个小片段(当然是匿名的):

[
{
"id": "1234",
"name": "Name1",
"url": "https://localhost/Name1",
"date_created": "2013-07-05T18:47:05Z",
"date_cancelled": "",
"props": [
{
"id": "54321",
"type": "Client",
"value": "General Store"
},
{
"id": "65432",
"type": "Contact_Name",
"value": "Joe Smith"
}
]
},
{
"id": "23456",
"name": "Name2",
"url": "https://localhost/Name2",
"date_created": "2014-02-27T17:46:43Z",
"date_cancelled": "",
"props": [
{
"id": "34567",
"type": "Client",
"value": "Bait Shop"
}
]
}]

这是相关代码:

var _ = require('underscore');
var recs = require('./prod.json');

printArr(recs);

console.log(recs.length);

function printArr(arr) {
arr.forEach(function(item) {
console.log(item + ", ");
});
}

任何指导将不胜感激。

更新:

好吧,显然问题出在我的 printArr 函数上。我不确定我在那里做错了什么。我想弄清楚它,因为我想对此进行扩展,以便我可以有选择地打印。

最佳答案

the parsed file just outputs [object, Object].

这是预期的行为,因为您正在将对象与字符串连接起来。

尝试使用 console.log(item) 代替

关于javascript - 使用node.js解析json时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com