Created
October 21, 2014 13:16
-
-
Save magicdawn/b9ea7b05c92913da439d to your computer and use it in GitHub Desktop.
JSON._parse not quoted key string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = parse | |
function parse(s) { | |
s = s.replace(/'/g, "\'") | |
.replace(/"/g, '\"') | |
.replace(/\n/g, '\\n') | |
.replace(/\r/, '\\r') | |
var code = "return " + s | |
var f = new Function(code) | |
return f() | |
} | |
var s = '{ "name": "zhang" }' | |
var ss = '{ name: "zhang" }' | |
console.log(parse(s)); | |
console.log(parse(ss)); | |
var o = parse(ss) | |
console.log(o.name); | |
/* | |
{ name: 'zhang' } | |
{ name: 'zhang' } | |
zhang | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
可能执行恶意代码...放在vm的sandbox里执行...