如何转换字符串
"{7: {type: 'line'},8: {type: 'line'}}"
进入Javascript OBJECT
{7: {type: 'line'},8: {type: 'line'}}
答案 0 :(得分:1)
使用JSON.parse
JSON.parse(STRING) // convert to object
JSON.stringify(STRING) // convert object to string, that can be used in JSON.parse
不要使用数字作为键,因为不好的做法是你无法访问这样的对象:
var a = {"8": "value"};
// Invalid syntax
console.log(a.8)
// work
console.log(a["8"], a[8]);
var b = {"eight" : "value"};
// work
console.log(b.eight);