0

I've collected JSON-responses before and yet this particular one I can't get the objects from no matter what tactics I use!

My JSON:

{nodes: [{title:"test" }, { title:"test2" }, {

title:"test3" }, {

title:"test4" }]};

My Js :

 $.post('http://mysite.com/some.php', data,  function (data) {


  for (var i=0, len=data.length; i < len; i++) {


      console.log(data.nodes[i].title);


};

});

I get the entire DATA as JSON and that works fine but I can't get the parse for the objects within to work, how should I formulate it? I've tried using "FOR" as well instead of "EACH"

MDDY
  • 215
  • 3
  • 10
  • 1
    possible duplicate of [I have a nested data structure / JSON, how can I access a specific value?](http://stackoverflow.com/questions/11922383/i-have-a-nested-data-structure-json-how-can-i-access-a-specific-value) – Felix Kling Apr 11 '13 at 23:43

2 Answers2

1

Those numbered keys suggest the nodes container should be an array, not an object

Kae Verens
  • 4,076
  • 3
  • 21
  • 41
  • Yes, this has been fixed - It was great that you pointed that out! :) However I still have the same problem as before :/ – MDDY Apr 12 '13 at 11:46
0

I agree with @KaeVerens, this is (or was) badly structured JSON. Besides the array that isn't an array, there's an extra level of object you don't appear to need. If you're generating the JSON, it would make a lot more sense if it were this instead:

{
    "nodes": [
        { "title": "test", "field2": "test2" },
        { "title": "test3", "field2": "test4" }
    ]
}

OK, updating now that your JSON data looks more like this. But now what you posted isn't valid JSON at all! It's a JavaScript object that will work in the console or in test code, but it won't pass a JSON validator. The property names aren't quoted as they should be, and there's a ; at the end. It should look like:

{
    "nodes": [
        { "title": "test" },
        { "title": "test2" },
        { "title": "test3" },
        { "title": "test4" }
    ]
}

(Spacing and indentation don't matter; I just formatted it this way for clarity.)

Anyway, with that fixed, this code works fine on your current data:

$.each( data.nodes, function( index, node ) {    
    console.log( node.title );
});

And your for loop would work fine if you check the length of the actual nodes array instead of checking data.length which doesn't exist:

var nodes = data.nodes;
for( var i = 0, len = nodes.length;  i < len;  i++ ) {
    var node = nodes[i];
    console.log( node.title );
}

I recommend taking references to nested objects as this code does (the nodes and node variables) instead of stuff like data.nodes[i]. Much easier to keep track of, and it can be more efficient too (although it wouldn't be any different in this simple case).

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • Thank you for the response! The thing is that I can get to "nodes" but I just get "TypeError: 'undefined' is not an object". So I can technically reach it but it says its the wrong object/array. I use: console.log(data.nodes); but it gives me the error @MichaelGeary – MDDY Apr 12 '13 at 11:38