使用 node.js 遍历 JSON [英] Looping through JSON with node.js
本文介绍了使用 node.js 遍历 JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个需要迭代的 JSON 文件,如下所示...
I have a JSON file which I need to iterate over, as shown below...
{
"device_id": "8020",
"data": [{
"Timestamp": "04-29-11 05:22:39 pm",
"Start_Value": 0.02,
"Abstract": 18.60,
"Editor": 65.20
}, {
"Timestamp": "04-29-11 04:22:39 pm",
"End_Value": 22.22,
"Text": 8.65,
"Common": 1.10,
"Editable": "true",
"Insert": 6.0
}]
}
数据中的键并不总是相同的(我刚刚使用了示例,有 20 个不同的键),因此,我无法设置我的脚本来静态引用它们以获取值.
The keys in data will not always be the same (i've just used examples, there are 20 different keys), and as such, I cannot set up my script to statically reference them to get the values.
否则我可以声明
var value1 = json.data.Timestamp;
var value2 = json.data.Start_Value;
var value3 = json.data.Abstract;
etc
过去我在数据节点上使用了一个简单的 foreach 循环......
In the past i've used a simple foreach loop on the data node...
foreach ($json->data as $key => $val) {
switch($key) {
case 'Timestamp':
//do this;
case: 'Start_Value':
//do this
}
}
但是不想阻塞脚本.有什么想法吗?
But don't want to block the script. Any ideas?
推荐答案
您可以通过以下方式遍历 JavaScript 对象:
You can iterate through JavaScript objects this way:
for(var attributename in myobject){
console.log(attributename+": "+myobject[attributename]);
}
myobject 可能是你的 json.data
myobject could be your json.data
这篇关于使用 node.js 遍历 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文