Parse JSON in JavaScript

The easiest way to parse a JSON-string.


Libraries

Code Snippet
function ParseJson(str) {
    str = JSON.stringify(str);
    if (typeof JSON.parse === "function") { 
        return JSON.parse(str);
    } else {
        return eval('(' + str + ')');
    }
}


Implementation

Use the function above to convert from a string to a JSON-Object even if the usually used function JSON.parse is not provided by the browser.<code>var parsed = ParseJson('{"name":"John Doe","age":"21"}'); parsed.name // returns John Doe parsed.age // returns 21</code>





Comments
  • mode_editWrite a comment



  • forumComments




Embed Code

We provide special containers which include the full snippet and a link to this site. So for example when you make a tutorial and you are using code from this site, you can use one of the following snippet embeddings:

Load Preview Get Code Close Preview







    Share
    Rating
    thumb_up 0 thumb_down
    Author
    Thorolus

    I'm the head developer and co-founder of GlovilGames Studios (glovilgames.com) and other platforms GlovilGames created, like vsnippets.com, worth-calculator.net, python-obfuscator.com and some mobile apps and games.

    Tags
    JSON
    Parse



    Informations

    posted on 2016-09-22 16:35:18

    viewed 82 times

    snippet's UUID is 57e3ec269d487

    Similar Snippets