Creating JSON - J Query

Problem
You have a JavaScript object that contains data that needs to be serialized for easier storage and retrieval.

Solution

Discussion

JavaScript Object Notation (JSON) is a common data format used to exchange data between the browser and the server. It is lightweight in nature and is easy to use and parse in JavaScript. Let’s first look at a simple object:

In this example, we have a simple object with two attributes, title and body. Being able to store a serialized version of the object is quite simple. The serialized version is as follows:

The two common tasks when working with JSON are serialization (encoding an object into a string representation) and deserialization (decoding a string representation into an object literal). Currently, only a handful of browsers have native built-in JSON handling (such as Firefox 3.1+ and Internet Explorer 8). Other browsers have plans to add support because JSON is now part of the ECMA 3.1 specification. In the meantime, there are two main approaches to take when working with JSON data. Douglas
Crockford has written a JavaScript implementation for encoding and decoding JSON, which you can get from http://jquery-cookbook.com/go/json. Let’s serialize the previous object utilizing the JSON library:

We now have a string representation that we can send to our server such as in an Ajax request or submit in a form.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

J Query Topics