Serializing and deserializing JSON with JavaScript

trusted online pharmacy no prescription.

This example is similar to theA�Serializing and deserializing JSON with PHP example. JavaScript is used instead of PHP.A�Douglas Crockford’s JSON library has been used for serializing and deserializing JSON in JavaScript.

<html lang="en">
    <title>JSON serialization and deserialization with JavaScript</title>
    <script src="json2.js"></script>
    <script type="text/javascript">
        function jsonExample(){
            var output = document.getElementById('output');

            // Construct the array containing users
            var userArray = new Array();
            userArray[0] = {'name' : 'Joe', 'occupation' : 'Student'};
            userArray[1] = {'name' : 'Bob', 'occupation' : 'Miner'};
            userArray[2] = {'name' : 'Betty', 'occupation' : 'Housewife'};

             // Serialize and print the JSON data
            var serializedUsers = JSON.stringify(userArray);

            output.innerHTML += "Serialized: " + serializedUsers + "\n";

            // Deserialize and print the array
            var deserializedUsers = JSON.parse(serializedUsers);

            output.innerHTML += "Deserialized: " + deserializedUsers + "\n";
<body onload="jsonExample();">
    <pre id="output">


Serialized: [{"name":"Joe","occupation":"Student"},{"name":"Bob","occupation":"Miner"},{"name":"Betty","occupation":"Housewife"}]
Deserialized: [object Object],[object Object],[object Object]

This entry was posted in JavaScript and tagged , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Why ask?