Created
July 11, 2019 03:48
-
-
Save Jason-cqtan/7ed007a93e52fbac562d95e6848ab3da to your computer and use it in GitHub Desktop.
判断字符串是否json字符串
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
if(typeof str == 'string'){ | |
try{ | |
var obj = JSON.parse(str); | |
return !!obj && typeof obj === 'object'; | |
}catch(e){ | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
需考虑
JSON.parse('123'); // 123
JSON.parse('{}'); // {}
JSON.parse('true'); // true
JSON.parse('"foo"'); // "foo"
JSON.parse('[1, 5, "false"]'); // [1, 5, "false"]
JSON.parse('null'); // null
如有需要使用validator库