isJsonString('{ "Id": 1, "Name": "Coke" }')
应该是正确的
isJsonString('foo')
isJsonString('<div>foo</div>')
应该为假。
我正在寻找一种不使用try/catch的解决方案,因为我将调试器设置为“在所有错误上中断”,这导致它在无效的JSON字符串上中断。
isJsonString('{ "Id": 1, "Name": "Coke" }')
应该是正确的
isJsonString('foo')
isJsonString('<div>foo</div>')
应该为假。
我正在寻找一种不使用try/catch的解决方案,因为我将调试器设置为“在所有错误上中断”,这导致它在无效的JSON字符串上中断。
当前回答
也许它会有用:
function parseJson(code)
{
try {
return JSON.parse(code);
} catch (e) {
return code;
}
}
function parseJsonJQ(code)
{
try {
return $.parseJSON(code);
} catch (e) {
return code;
}
}
var str = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5}";
alert(typeof parseJson(str));
alert(typeof parseJsonJQ(str));
var str_b = "c";
alert(typeof parseJson(str_b));
alert(typeof parseJsonJQ(str_b));
输出:
IE7:字符串、对象、字符串,字符串
铬:对象、对象、字符串,字符串
其他回答
var jsonstring='[{“ConnectionString”:“aaaaa”,“Server”:“ssssss”}]'; if(((x)=>{try{JSON.parse(x);return true;}catch(e){return false}})(jsonstring)){ document.write(“valide json”) }else{ document.write(“invalide json”) }
你可以使用javascript的eval()函数来验证它是否有效。
e.g.
var jsonString = '{ "Id": 1, "Name": "Coke" }';
var json;
try {
json = eval(jsonString);
} catch (exception) {
//It's advisable to always catch an exception since eval() is a javascript executor...
json = null;
}
if (json) {
//this is json
}
或者,您也可以使用JSON。解析函数来自json.org:
try {
json = JSON.parse(jsonString);
} catch (exception) {
json = null;
}
if (json) {
//this is json
}
希望这能有所帮助。
警告:如果有人添加了恶意的JS代码,eval()是危险的,因为它会执行它。确保JSON字符串是可信的,即你从一个可信的来源。
对于我的第一个解决方案,建议这样做。
try {
json = eval("{" + jsonString + "}");
} catch (exception) {
//It's advisable to always catch an exception since eval() is a javascript executor...
json = null;
}
保证json的一致性。如果jsonString不是纯JSON, eval将抛出异常。
使用JSON.parse(str)的IsJsonString(str)函数在我的例子中不起作用。 我试图从GraphiQL验证json输出,它总是返回false。幸运的是,isJSON工作得更好:
var test = false;
$('body').on('DOMSubtreeModified', '.resultWrap', function() {
if (!test) {
var resultWrap = "{" + $('#graphiql .resultWrap').text().split("{").pop();
if isJSON(resultWrap) {test = !test;}
console.log(resultWrap);
console.log(resultWrap.isJSON());
}
});
样例输出:
THREE.WebGLRenderer 79
draw.js:170 {xxxxxxxxxx
draw.js:170 false
draw.js:170 {xxxxxxxxxx
draw.js:170 false
draw.js:170 {xxxxxxxxxx
draw.js:170 false
draw.js:170 {xxxxxxxxxx
draw.js:170 false
draw.js:170 {
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56, "fps": 41.666666666666664,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56, "fps": 41.666666666666664, "width": 396.984,
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56, "fps": 41.666666666666664, "width": 396.984, "height": 327
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56, "fps": 41.666666666666664, "width": 396.984, "height": 327}
draw.js:170 false
draw.js:170 { "PI": 3.141592653589793, "time": 1570751209006, "tick": 156, "tickr": 1.56, "fps": 41.666666666666664, "width": 396.984, "height": 327}
draw.js:170 true
我来得太晚了。这就是我最后做的事。 使用快速的regex预检查可以大大提高性能
if(/^\s*(\{|\[)/.test(str)){
try{
JSON.parse(str)
// do something here, or return obj/true
}catch(e){
// do nothing or return false
}
}
正则表达式将检查字符串是否以[或{开头。 这将消除大多数错误情况(不是全部)。 下面是一个快速的性能测试https://jsbench.me/awl6fgn8jb/1
最坏的情况下,这可能比直接使用try慢10-15%,最坏的情况意味着所有字符串都是有效的json字符串。
最好的情况下这比纯try快99%,最好的情况下意味着所有字符串都是无效的json。
if(resp) {
try {
resp = $.parseJSON(resp);
console.log(resp);
} catch(e) {
alert(e);
}
}
希望这也适用于你