Convert JS object to JSON string

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

    convert str => obj

    const onePlusStr = '[{"brand":"oneplus"},{"model":"7T"}]';

    const onePLusObj = JSON.parse(onePlusStr);

    convert obj => str

    const onePLusObjToStr = JSON.stringify(onePlusStr);

    References of JSON parsing in JS:
    JSON.parse() : click
    JSON.stringify() : click

提交回复
热议问题