我已经在数据库中存储了一个编码的字符串JSON对象,我对它进行了解码和加载,并试图将它解析为一个对象,但我得到了
Uncaught : JSON.parse ()位置2处JSON中的意外令牌
代码:
var attr = new Object();
attr = JSON.parse(code[1].replace(/"/g, "'"));
已解码的物体:
[ {'inputI':0,'type':'variable'},{'inputD':0,'type':'variable'},{‘inputI’:0,'type':'variable'},{'paras':0,'type':'variable'},{'headers':0,'type':'variable'},{‘variable’:0,'type':'variable'},{'lists':0,'type':'variable'},{‘type’:‘variable’}{'divs':0,'type':'variable'},{'links':0,'type':'variable'},{‘映像’:0,'type':'variable'},{'elemName':'{}','type':'object'},{‘B边界’:[],'type':'array'},{‘嵌套’:[],‘type’:‘数组’},{‘links’}:[],'type':'array'},{“工具提示”:[],“类型”:“数组”},{“梯度颜色”:“{}”,“类型”:“object”},{“events”:{{},“type”:‘object’},{‘object’},{'sTarget':'{}','type':'object'},{'sMain':'{}','type':'object'},{‘orignalStyle’:{},'type':'object'},{'objNewStyle':'{}','type':'object'},{'reverseFunction':'{}','type':'object'},{'scDetails':'{}',‘type’:‘object’}}]
发布于 2017-07-01 04:13:07
这是无效 json字符串。它的值和键应该用双引号(而不是单引号)包围。因此,当您执行.replace(/"/g, "'")
时,基本上违反了JSON标准。
值可以是双引号中的字符串,也可以是数字,也可以是真、假或空,也可以是对象或数组。这些结构可以嵌套。
发布于 2018-06-06 10:18:39
“我也有同样的错误,”Philipp Zitzmann是正确的。必须在https://jsonformatter.curiousconcept.com/上使用有效的json字符串。
有效的json字符串必须有双引号。
JSON.parse({"u1":1000,"u2":1100}) // will be ok
无引号引起错误
JSON.parse({u1:1000,u2:1100})
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2
单引号引起误差
JSON.parse({'u1':1000,'u2':1100})
// error Uncaught SyntaxError: Unexpected token u in JSON at position 2
发布于 2017-07-01 04:14:45
https://stackoverflow.com/questions/44860697
复制