Skip to content

Instantly share code, notes, and snippets.

@ravishakya
Created September 19, 2016 10:52
Show Gist options
  • Select an option

  • Save ravishakya/4f5dda63dbe945e103e0c32ab0e453a2 to your computer and use it in GitHub Desktop.

Select an option

Save ravishakya/4f5dda63dbe945e103e0c32ab0e453a2 to your computer and use it in GitHub Desktop.

Revisions

  1. ravishakya created this gist Sep 19, 2016.
    15 changes: 15 additions & 0 deletions Check json or not ( javascript ).js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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;
    };