javascript - JSON.stringify on nested object ignores nested objects - Stack Overflow

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

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

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: /

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

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/VJTaV/

Share Improve this question edited Mar 27, 2014 at 14:55 user2345998 asked Mar 27, 2014 at 13:57 user2345998user2345998 6491 gold badge13 silver badges32 bronze badges 2
  • 3 How are you building this object? I created the same object, and JSON.stringify works fine: jsfiddle/H8CF2 – gen_Eric Commented Mar 27, 2014 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 Commented Mar 27, 2014 at 14:56
Add a ment  | 

2 Answers 2

Reset to default 4

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.

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
});

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信