Created
March 2, 2022 00:20
-
-
Save lvxianchao/149098571df874c2706d1b30c8178445 to your computer and use it in GitHub Desktop.
JS 判断字符串是否是 JSON
This file contains 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
function isJson(str) { | |
if (typeof str === 'string') { | |
try { | |
let obj = JSON.parse(str); | |
return !!(typeof obj === 'object' && obj); | |
} catch (e) { | |
return false; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment