Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I'm genereating an object which looks like that when you inspect it with Chrome:

object in Chrome inspector

When I try to stringify this object with JSON.stringify I get the following result:

{
    "results" : [{
            "ID" : 1,
            "NAME" : "Admin"
        }, {
            "ID" : 2,
            "NAME" : "Standarduser"
        }, {
            "ID" : 3,
            "NAME" : "Consultant"
        }, {
            "ID" : 4,
            "NAME" : "Leergruppe"
        }
    ]
}

For some reason all the nested nodes are missing. I'm sure this is pretty simple and obvisious, but at the moment I simply can't find my mistake. Many thanks for you help!

EDIT: JSFiddle how the JSON is created: http://jsfiddle.net/VJTaV/

share|improve this question
3  
How are you building this object? I created the same object, and JSON.stringify works fine: jsfiddle.net/H8CF2 – Rocket Hazmat Mar 27 '14 at 14:38
    
Hi Rocket, I've edited my question and created an entry on jsfiddle. It's not runnable but I guess it's enough to get the idea how it's created. – user2345998 Mar 27 '14 at 14:56
up vote 4 down vote accepted

Since your jsfiddle shows an ajax call, it is likely that you have called JSON.stringify() before the ajax results are available. You will need to put it in the success callback to be executed after the ajax finished.

Chrome inspector will show the properties as when you're modifying an object after it was logged you later can expand and see the new properties that have not yet existed at the time of the console.log call.

share|improve this answer
    
Thanks, of course that was the reason. For now I've changed the .get call to an .ajax call and made this synchronous. – user2345998 Mar 28 '14 at 8:46

As stated by Bergi, you are trying to stringify data not loaded yet, because $.get calls are asynchronous. You can attach .done callback to each of them, or, if you want execute a code only when all request are finished, use jQuery.when :

var jqXHRs = [];

$( data.results ).each(function( key, val ) {
  var jqXHR = $.get( userDataUrl, "", function( res ) {
    // ...
  });

  jqXHRs.push(jqXHR);
});

$.when.apply( $, jqXHRs ).done(function() {
    console.log( val ); // will log all nested nodes
});
share|improve this answer
    
Thanks a lot for this input, but one question though: You write I could attach .done to each request - but I can't see the difference to using the success-callback at this point. – user2345998 Mar 28 '14 at 8:52
    
success callback is the old API, it is now recommended to use .done instead, but result is quite the same. – Anthony Garcia Mar 28 '14 at 9:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.