O'Reilly logo

HTML5 Data and Services Cookbook by Mite Mitreski, Gorgi Kosev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Serializing objects to a JSON string

The reverse of the previous recipe is to serialize JavaScript objects into JSON string. Similarly the same rules about having browser support for JSON applies but again this is not a problem in most of the browsers. One way would be to manually create the string, but that is just a way to error-prone and messy browsers, so we will try out some of the methods available out there.

How to do it...

In the following example we use only JavaScript, so we can place it inside a simple script tag in an HTML file:

  1. First need data in order to serialize it to string, so we will create a simple JavaScript object:
        var someJSON = {
          "firstname":"John",
          "lastname":"Doe",
          "email":"john.doe@example.com"
         };
  2. We create another object ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required