Created
September 19, 2016 10:52
-
-
Save ravishakya/4f5dda63dbe945e103e0c32ab0e453a2 to your computer and use it in GitHub Desktop.
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 tryParseJSON(jsonString){ | |
try { | |
var o = JSON.parse(jsonString); | |
// Handle non-exception-throwing cases: | |
// Neither JSON.parse(false) or JSON.parse(1234) throw errors, hence the type-checking, | |
// but... JSON.parse(null) returns null, and typeof null === "object", | |
// so we must check for that, too. Thankfully, null is falsey, so this suffices: | |
if (o && typeof o === "object") { | |
return o; | |
} | |
} | |
catch (e) { } | |
return false; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment