關於 eval 的報錯 Uncaught ReferenceError: False is not defined

var obj ={'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}
VM3614:1 Uncaught ReferenceError: False is not defined
at <anonymous>:1:47
(anonymous) @ VM3614:1
var obj
undefined
obj ={'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}
VM3633:1 Uncaught ReferenceError: False is not defined
at <anonymous>:1:43
(anonymous) @ VM3633:1
obj = "{'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}"
"{'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}"
alert(obj)
undefined
var oo =eval("("+obj+")")
VM3699:1 Uncaught ReferenceError: False is not defined
at eval (eval at <anonymous> ((index):1), <anonymous>:1:39)
at <anonymous>:1:9
(anonymous) @ VM3699:1
(anonymous) @ VM3698:1
var oo =eval(obj)
VM3724:1 Uncaught SyntaxError: Unexpected token :
at <anonymous>:1:14
(anonymous) @ VM3723:1
obj
"{'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}"
typeof obj
"string"
obj ={'id': 16, 'name': '管理員', 'delflag': False, 'grade': 1000000.0}
VM3755:1 Uncaught ReferenceError: False is not defined
at <anonymous>:1:43
(anonymous) @ VM3755:1
obj ={'id': 16, 'name': '管理員', 'grade': 1000000.0}
{id: 16, name: "管理員", grade: 1000000}
alert(obj)
undefined
var test =eval("("+obj+")")
VM3847:1 Uncaught SyntaxError: Unexpected identifier
at <anonymous>:1:23
(anonymous) @ VM3846:1
var test =eval("("+"obj"+")")
undefined
typeof test
"object"
alert(test)
undefinedjson

緣由是:字典裏存儲了一個False的變量,js沒法識別,SO  報錯,ide

更重要的是,由於不是正規的json格式,因此會報錯,正規的 key 是必需要有雙引號的。(即便是單引號也不行。)spa

 

錯誤代碼:token

 

var res = "[{id:1,name:'liming'},{id:2,name:'xiaobai'}]";
var objRes = JSON.parse(res);
console.log(objRes)
1
2
3
正確代碼:string

 

var res = '[{"id":1,"name":"liming"},{"id":2,"name":"xiaobai"}]';
var objRes = JSON.parse(res);
console.log(objRes)


console

相關文章
相關標籤/搜索