Click here to Skip to main content
14,974,357 members
Articles / General Programming / String
Technical Blog
Posted 12 Feb 2011

Stats

32.7K views
2 bookmarked

Serialize JSON Object to String

Rate me:
Please Sign up or sign in to vote.
4.20/5 (4 votes)
16 Feb 2011CPOL
Serialize JSON Object to String

A simple way to serialize a JSON object is to iterate through the object’s property and create a JSON formatted string. All the properties and functions in a JSON object can be read just like an associative array or a name/value pair. This allows us to list all the keys and query the object for the values using the keys. Look at the following script:

JavaScript
var myJSON = {
    FirstName: '',
    LastName: '',
    Email: '',
    load: function () {
        //implementation here
    },
    serialize: function () {
        var json = "";
        for (var key in this) {
            if (typeof this[key] != 'function') {
                json += (json != "" ? "," : "") + key + ":'" + this[key] + "'";
            }
        }
        json = '[{' + json + '}]';
        return json;
    }
}

The serialize function uses a for loop to get all the keys in the object. It checks if the type is not a function because, for this case, we just want to extract the user data values and ignore the functions. You could also extract the contents of the function by just removing the if condition. To build the formatted string, the function concatenates a string with the key/value pair until it reads all the keys. The following sample script sets the property values and serializes the data to a string:

JavaScript
//client code to set properties and serialize data
if (typeof (myJSON) != 'undefined') {
    myJSON.FirstName = "myfirstname";
    myJSON.LastName = "mylastname";
    myJSON.Email = "myemail";
    var data = myJSON.serialize();
}

The data variable contains a string with this format:

C#
[{FirstName:’myfristname’,LastName:’mylastname’,Email:’myemail’}]

I hope this helps.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ozkary
Architect OG-BITechnologies
United States United States
Software engineer, author & speaker who enjoys mentoring, learning, speaking and sharing with others about software development technologies. Microsoft MVP.

My Blog

Comments and Discussions

 
GeneralMy vote of 1 Pin
Steven Quick22-Feb-11 10:34
MemberSteven Quick22-Feb-11 10:34 
GeneralMy vote of 5 Pin
justinonday16-Feb-11 19:41
Memberjustinonday16-Feb-11 19:41 
GeneralMy vote of 4 Pin
justinonday16-Feb-11 19:41
Memberjustinonday16-Feb-11 19:41 
GeneralMy vote of 5 Pin
ozkary16-Feb-11 3:29
Memberozkary16-Feb-11 3:29 
GeneralProblems with quoted strings etc Pin
jsc4215-Feb-11 6:33
professionaljsc4215-Feb-11 6:33 
GeneralRe: Problems with quoted strings etc Pin
ozkary15-Feb-11 9:19
Memberozkary15-Feb-11 9:19 
Hi,

Thanks for the feedback. The blog was written without any quoted string.There is a problem with the way the RSS feed was consumed. The code should read:

if (typeof (myJSON) != 'undefined') {
myJSON.FirstName = "myfirstname";
myJSON.LastName = "mylastname";
myJSON.Email = "email@cn.com";
var data = myJSON.serialize();
}

thanks.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.