字符串对象转对象出错

282 阅读1分钟

JSON.parse()出错

var str='{"hideSide":"hideSide","smLogo":{"w":"11px","h":"11px"},"bigLogo":{"w":"11px","h":"11px"}}';
var str1="{'hideSide':'hideSide','smLogo':{'w':'11px','h':'11px'},'bigLogo':{'w':'11px','h':'11px'}}";
console.log(str,JSON.parse(str),str1,'\n',JSON.parse(str1))
出现下图错误,原因是json的标准格式key和value都必须加双引号而不是单引号;

VM551:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1
    at JSON.parse (<anonymous>)