Looping through JSON with node.js

前端 未结 9 1639
感情败类
感情败类 2020-12-08 00:04

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         


        
9条回答
  •  既然无缘
    2020-12-08 00:50

    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:

    for(var i = 0; i < jsonArray.length; i++)
    {
        console.log(jsonArray[i].attributename);
    }
    

    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:

    var keys = Object.keys(jsonObject);
    for(var i = 0; i < keys.length; i++) 
    {
        var key = keys[i];
        console.log(jsonObject.key.attributename);
    }
    

提交回复
热议问题