使用 Node.js 循环 JSON
我有一个需要迭代的 JSON 文件,如下所示...
{
"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 个不同的键),因此,我无法设置启动我的脚本以静态引用它们以获取值。
否则我可以说
var value1 = json.data.Timestamp;
var value2 = json.data.Start_Value;
var value3 = json.data.Abstract;
etc
过去我在数据节点上使用了一个简单的 foreach 循环...
foreach ($json->data as $key => $val) {
switch($key) {
case 'Timestamp':
//do this;
case: 'Start_Value':
//do this
}
}
但不想阻止脚本。有什么想法吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以通过这种方式迭代 JavaScript 对象:
myobject 可以是您的 json.data
You can iterate through JavaScript objects this way:
myobject could be your json.data
我建议利用 NodeJS 始终是 ES5 的事实。请记住,这不是浏览器,您可以依赖该语言的实现来保持稳定。也就是说,我建议不要在 NodeJS 中使用 for-in 循环,除非你真的想在原型链上进行深度递归。对于简单、传统的循环,我建议充分利用 ES5 中的 Object.keys 方法。如果您查看以下 JSPerf 测试,特别是如果您使用 Chrome(因为它具有相同的引擎(如 NodeJS),您将大致了解使用此方法比使用 for-in 循环性能提高多少(大约快 10 倍)。这是代码示例:
I would recommend taking advantage of the fact that nodeJS will always be ES5. Remember this isn't the browser folks you can depend on the language's implementation on being stable. That said I would recommend against ever using a for-in loop in nodeJS, unless you really want to do deep recursion up the prototype chain. For simple, traditional looping I would recommend making good use of Object.keys method, in ES5. If you view the following JSPerf test, especially if you use Chrome (since it has the same engine as nodeJS), you will get a rough idea of how much more performant using this method is than using a for-in loop (roughly 10 times faster). Here's a sample of the code:
您可能还想在循环中使用 hasOwnProperty 。
node.js 是单线程的,这意味着无论您是否愿意,您的脚本都会阻塞。还记得 V8(node.js 使用的 Google Javascript 引擎)将 Javascript 编译为机器代码,这意味着大多数基本操作都非常快,并且循环遍历具有 100 个键的对象可能需要几纳秒?
但是,如果您在循环内执行了更多操作,并且不希望它立即阻塞,则可以执行以下操作:
如果您的循环正在执行一些 CPU 密集型工作,则需要生成一个子进程来完成这项工作或使用网络工作者。
You may also want to use hasOwnProperty in the loop.
node.js is single-threaded which means your script will block whether you want it or not. Remember that V8 (Google's Javascript engine that node.js uses) compiles Javascript into machine code which means that most basic operations are really fast and looping through an object with 100 keys would probably take a couple of nanoseconds?
However, if you do a lot more inside the loop and you don't want it to block right now, you could do something like this
If your loop is doing some very CPU intensive work, you will need to spawn a child process to do that work or use web workers.
我最喜欢的方式是,
My most preferred way is,
如果您想避免阻塞(这仅对非常大的循环来说是必要的),请将循环的内容包装在一个如下所示的函数中:
process.nextTick(function(){<循环内容>})< /code>,这会将执行推迟到下一个时钟周期,从而为来自其他异步函数的待处理调用提供了处理的机会。
If you want to avoid blocking, which is only necessary for very large loops, then wrap the contents of your loop in a function called like this:
process.nextTick(function(){<contents of loop>})
, which will defer execution until the next tick, giving an opportunity for pending calls from other asynchronous functions to be processed.有点晚了,但我相信下面给出了一些进一步的说明。
您也可以使用简单的循环来遍历 JSON 数组,例如:
如果您有一个 JSON 对象并且想要循环遍历其所有内部对象,那么您首先需要获取数组中的所有键并循环遍历键使用键名称检索对象,例如:
A little late but I believe some further clarification is given below.
You can iterate through a JSON array with a simple loop as well, like:
If you have a JSON object and you want to loop through all of its inner objects, then you first need to get all the keys in an array and loop through the keys to retrieve objects using the key names, like:
不确定它是否有帮助,但看起来这里托管的节点中可能有一个用于异步迭代的库:
https://github.com/caolan/async
Not sure if it helps, but it looks like there might be a library for async iteration in node hosted here:
https://github.com/caolan/async
看看特拉弗斯。它将为您递归地遍历对象树,并且在每个节点上您都有许多可以访问的不同对象 - 当前节点的键、当前节点的值、当前节点的父节点、当前节点的完整键路径等。 https://github.com/substack/js-traverse。当我需要在转换各种数据位时进行深度克隆时,我使用它对想要清除循环引用的对象产生了良好的效果。下面是从他们的示例中提取的一些代码,让您了解它的功能。
Take a look at Traverse. It will recursively walk an object tree for you and at every node you have a number of different objects you can access - key of current node, value of current node, parent of current node, full key path of current node, etc. https://github.com/substack/js-traverse. I've used it to good effect on objects that I wanted to scrub circular references to and when I need to do a deep clone while transforming various data bits. Here's some code pulled form their samples to give you a flavor of what it can do.