Convert JS object to JSON string

后端 未结 27 2494
庸人自扰
庸人自扰 2020-11-22 00:43

If I defined an object in JS with:

var j={\"name\":\"binchen\"};

How can I convert the object to JSON? The output string should be:

27条回答
  •  无人共我
    2020-11-22 01:46

    Just use JSON.stringify to do such conversion - however remember that fields which have undefined value will not be included into json

    var j={"name":"binchen", "remember":undefined, "age": null };
    
    var s=JSON.stringify(j);
    
    console.log(s);

    The field remember 'disappear' from output json

提交回复
热议问题