Convert JS object to JSON string

后端 未结 27 2493
庸人自扰
庸人自扰 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:24

    JSON.stringify turns a Javascript object into JSON text and stores that JSON text in a string.

    The conversion is an Object to String

    JSON.parse turns a string of JSON text into a Javascript object.

    The conversion is a String to Object

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

    to make it a JSON String following could be used.

    JSON.stringify({"key":"value"});
    
    JSON.stringify({"name":"binchen"});
    

    For more info you can refer to this link below.

    https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

提交回复
热议问题