Convert JS object to JSON string

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

    if you want to get json properties value in string format use the following way

    var i = {"x":1}
    
    var j = JSON.stringify(i.x);
    
    var k = JSON.stringify(i);
    
    console.log(j);
    
    "1"
    
    console.log(k);
    
    '{"x":1}'
    

提交回复
热议问题