Javascript – JS Loop through a json Object

javascriptjsonloopsrecursiontitanium

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 🙂

Best Solution

Working FIDDLE Demo

I think that your JSON is very complex 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.