解析"轻松"JSON但避免邪恶的最简单方法是什么eval?
以下引发错误:
JSON.parse("{muh: 2}");
Run Code Online (Sandbox Code Playgroud)
因为正确的JSON应该引用键: {"muh": 2}
我的用例是一个简单的测试接口,用于将JSON命令写入节点服务器.到目前为止,我只是使用eval它,因为它只是一个测试应用程序.但是,在整个项目中使用JSHint总是让我烦恼不已eval.所以我想要一个安全的选择,仍然允许放宽键的语法.
PS:我不想仅仅为了测试应用而自己编写解析器:-)
Ase*_*ore 21
你已经知道了,因为你在这里引用我 = D,但我认为在这里记录它可能是好的:
我一直渴望能够编写仍然有效JS的"轻松"JSON,所以我采用了Douglas Crockford的无eval json_parse.js并扩展它以支持ES5功能:
https://github.com/aseemk/json5
此模块在npm上可用,可用作本机JSON.parse()方法的替代品.(它stringify()输出常规JSON.)
希望这可以帮助!=)
Arn*_*eil 20
您可以使用正则表达式替换来清理JSON:
var badJson = "{muh: 2}";
var correctJson = badJson.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ');
JSON.parse(correctJson);
Run Code Online (Sandbox Code Playgroud)
Mal*_*ous 10
这就是我最终要做的事情.我扩展了@ ArnaudWeil的答案,并增加了对:出现在值中的支持:
var badJSON = '{one : "1:1", two : { three: \'3:3\' }}';
var fixedJSON = badJSON
// Replace ":" with "@colon@" if it's between double-quotes
.replace(/:\s*"([^"]*)"/g, function(match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
})
// Replace ":" with "@colon@" if it's between single-quotes
.replace(/:\s*'([^']*)'/g, function(match, p1) {
return ': "' + p1.replace(/:/g, '@colon@') + '"';
})
// Add double-quotes around any tokens before the remaining ":"
.replace(/(['"])?([a-z0-9A-Z_]+)(['"])?\s*:/g, '"$2": ')
// Turn "@colon@" back into ":"
.replace(/@colon@/g, ':')
;
console.log('Before: ' + badJSON);
console.log('After: ' + fixedJSON);
console.log(JSON.parse(fixedJSON));Run Code Online (Sandbox Code Playgroud)
它产生这个输出:
Before: {one : "1:1", two : { three: '3:3' }}
After: {"one": "1:1", "two": { "three": "3:3" }}
{
"one": "1:1",
"two": {
"three": "3:3"
}
}
Run Code Online (Sandbox Code Playgroud)
如果在编写字符串时无法引用键,则可以在使用JSON.parse-之前插入引号
var s= "{muh: 2,mah:3,moh:4}";
s= s.replace(/([a-z][^:]*)(?=\s*:)/g, '"$1"');
var o= JSON.parse(s);
/* returned value:[object Object] */
JSON.stringify(o)
/* returned value: (String){
"muh":2, "mah":3, "moh":4
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19369 次 |
| 最近记录: |