JSON.stringify returns “[object Object]” instead of the contents of the object

前端 未结 4 927
梦如初夏
梦如初夏 2020-12-30 19:28

Here I\'m creating a JavaScript object and converting it to a JSON string, but JSON.stringify returns \"[object Object]\" in this case, instead of

相关标签:
4条回答
  • 2020-12-30 19:33

    Use

    var theObject = {name:{firstName:"Mark", lastName:"Bob"}};
    alert(JSON.stringify(theObject));
    
    0 讨论(0)
  • 2020-12-30 19:34

    Use alert(JSON.stringify(theObject));

    0 讨论(0)
  • 2020-12-30 19:49

    JSON.stringify returns "[object Object]" in this case

    That is because you are calling toString() on the object before serializing it:

    JSON.stringify(theObject.toString()) /* <-- here */
    

    Remove the toString() call and it should work fine:

    alert( JSON.stringify( theObject ) );
    
    0 讨论(0)
  • 2020-12-30 19:52
    theObject.toString()
    

    The .toString() method is culprit. Remove it; and the fiddle shall work: http://jsfiddle.net/XX2sB/1/

    0 讨论(0)
提交回复
热议问题