Converting an object to a string

后端 未结 30 2281
北荒
北荒 2020-11-22 03:29

How can I convert a JavaScript object into a string?

Example:

var o = {a:1, b:2}
console.log(o)
console.log(\'Item: \' + o)

Output:

30条回答
  •  没有蜡笔的小新
    2020-11-22 04:10

    I would recommend using JSON.stringify, which converts the set of the variables in the object to a JSON string. Most modern browsers support this method natively, but for those that don't, you can include a JS version:

    var obj = {
      name: 'myObj'
    };
    
    JSON.stringify(obj);
    

提交回复
热议问题