javascript - JS Loop through a json Object - Stack Overflow

i am trying to iterate a json object (in javascript) but it doesn't seem to work correctly... it&#

i am trying to iterate a json object (in javascript) but it doesn't seem to work correctly... it's very confusing for me to write a recursiv function, so maybe one of the experts here could help me :)

The json Object:

{
  "Node": [
    {
      "Name": {
        "#text": "Folder"
      }
    },
    {
      "Name": {
        "#text": "Folder 2"
      }
    },
    {
      "Name": {
        "#text": "Folder 3"
      },
      "Nodes": {
        "Node": {
          "Name": {
            "#text": "Folder 3.1"
          },
          "Nodes": {
            "Node": [
              {
                "Name": {
                  "#text": "Folder 3.1.1"
                },
                "Nodes": {
                  "Node": {
                    "Name": {
                      "#text": "Folder 3.1.1.1"
                    }
                  }
                }
              },
              {
                "Name": {
                  "#text": "Test 2"
                }
              }
            ]
          }
        }
      }
    },
    {
      "Name": {
        "#text": "Folder 4"
      }
    }
  ]
}

My try to solve the problem

function newFolder(_data) {    
    for (var i = 0; i < _data.length; i++) {      
        if (_data[i].Nodes) {        
            Ti.API.info("Sub: "); //+ _data[i].Nodes.Node.length );
                    
            return newFolder(_data[i].Nodes.Node);      
        } else {        
            Ti.API.info("Main: " + _data[i].Name["#text"]);      
        }      
        Ti.API.info("Main: " + _data[i].Name["#text"]);    
    }  
}

The problem is, that the functions does not run through each element, like i want to.

i've read something about jQuery each but i'm not very familar with that. plus i am using Titanium and i don't know exactly if i can use jquery.

it would be soo awesome if someone can help me out of this :)

i am trying to iterate a json object (in javascript) but it doesn't seem to work correctly... it's very confusing for me to write a recursiv function, so maybe one of the experts here could help me :)

The json Object:

{
  "Node": [
    {
      "Name": {
        "#text": "Folder"
      }
    },
    {
      "Name": {
        "#text": "Folder 2"
      }
    },
    {
      "Name": {
        "#text": "Folder 3"
      },
      "Nodes": {
        "Node": {
          "Name": {
            "#text": "Folder 3.1"
          },
          "Nodes": {
            "Node": [
              {
                "Name": {
                  "#text": "Folder 3.1.1"
                },
                "Nodes": {
                  "Node": {
                    "Name": {
                      "#text": "Folder 3.1.1.1"
                    }
                  }
                }
              },
              {
                "Name": {
                  "#text": "Test 2"
                }
              }
            ]
          }
        }
      }
    },
    {
      "Name": {
        "#text": "Folder 4"
      }
    }
  ]
}

My try to solve the problem

function newFolder(_data) {    
    for (var i = 0; i < _data.length; i++) {      
        if (_data[i].Nodes) {        
            Ti.API.info("Sub: "); //+ _data[i].Nodes.Node.length );
                    
            return newFolder(_data[i].Nodes.Node);      
        } else {        
            Ti.API.info("Main: " + _data[i].Name["#text"]);      
        }      
        Ti.API.info("Main: " + _data[i].Name["#text"]);    
    }  
}

The problem is, that the functions does not run through each element, like i want to.

i've read something about jQuery each but i'm not very familar with that. plus i am using Titanium and i don't know exactly if i can use jquery.

it would be soo awesome if someone can help me out of this :)

Share Improve this question edited May 15, 2013 at 12:55 Lee Taylor 7,98416 gold badges37 silver badges53 bronze badges asked May 15, 2013 at 12:49 Nico BarelmannNico Barelmann 211 gold badge1 silver badge3 bronze badges 5
  • 2 Why have you mented out your code?! – Lee Taylor Commented May 15, 2013 at 12:50
  • Its mented out for one. I am going to assume that is a typo. Once you parse the JSON with JSON.parse, you can loop through it. Now, depending on what it is, is how you will loop. If you the JSON is an array of objects, then you can iterate over it using the traditional for loop. However, if it is an object and you are trying to iterate over the properties of the object, you will have to use the for...in loop bined with the obj.hasOwnProperty check. – Jeff Shaver Commented May 15, 2013 at 12:53
  • There's no such thing as a JSON object. – nnnnnn Commented May 15, 2013 at 12:54
  • Hey thanks so far - my goal is to get an array containing all folders and their subfolders. But even the for in just loops through the first "layer" doesn't it? – Nico Barelmann Commented May 15, 2013 at 12:57
  • Your OBJECT is very bad. Can we changed it to a better one? – user1823761 Commented May 15, 2013 at 13:06
Add a ment  | 

3 Answers 3

Reset to default 4

Working FIDDLE Demo

I think that your JSON is very plex as there is no need. If you have an object like this:

var data = {
    "nodes": [
        { "name": "Folder 1" },
        { "name": "Folder 2" },
        { "name": "Folder 3" },
        {
            "name": "Folder 4",
            "nodes": [
                { "name": "Folder 4.1" },
                {
                    "name": "Folder 4.2",
                    "nodes": [
                        { "name": "Folder 4.2.1" },
                        { "name": "Folder 4.2.2" },
                        { "name": "Folder 4.2.3" }
                    ]
                },
                { "name": "Folder 4.3" }
            ]
        },
        { "name": "Folder 5" }
    ]

};

You can iterate over it by a recursive function:

function iterateNodes(data) {
    for (var i = 0, l = data.nodes.length; i < l; i++) {
        var node = data.nodes[i];

        console.log(node.name);

        if (node.nodes) {
            arguments.callee(node);
        }
    }
}

iterateNodes(data);

Check the FIDDLE Demo.

There is no thing as a JSON object. JSON is a way to format data. You are trying to go through an ordinary javascript object. You can look how in the first answer here

JSON is multi array , we can use data[a][b]... to get it.

function xmlhttprequest(url) {
 var xhr = new XMLHttpRequest();
 xhr.onreadystatechange = function() {
  if (xhr.readyState == 4) {
   if (xhr.status == 200) {
    var data = JSON.parse(xhr.responseText);
    console.log(data);
    for(var a in data){
     document.write(a+':<br>');
     for(var b in data[a]){
      document.write('&nbsp;&nbsp;&nbsp;'+b+':<br>');
      for(var c in data[a][b]){
       document.write('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+c+"="+data[a][b][c]+'<br>');
      }
     }
     document.write('<br>');
    }           
   }else{
    alert(url+'\n错误');
   }
  }
 }
 xhr.open('GET', url, false);
 xhr.send();
};
xmlhttprequest('https://api.shuax./tools/getchrome/json');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744232808a4564335.html

相关推荐

  • javascript - JS Loop through a json Object - Stack Overflow

    i am trying to iterate a json object (in javascript) but it doesn't seem to work correctly... it&#

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信