I am using to a jQuery post method to send some data to a server. Rather than constructing the JSON string myself, I would like to simply use a Javascript object. When I get the return string (in JSON) I would like to automatically construct a corresponding javascript object. Is this possible? Thanks in advance.
Javascript Object Serialization Definition
In this article we will examine the benefits of object serialization, the current browser implementations, and develop some code that could help your Ajax-based projects.
Assume we have a fairly complex JavaScript object defined using literal notation:
We can access any of the object properties in a variety of ways:
This object can also be passed to JavaScript functions and methods rather than specifying individual arguments. Useful stuff.
However, what if we need to store this object in a cookie? What if we need to pass the object to a web services via an Ajax request? What if that web service wants to return a modified version of the object? The answer is serialization:
Perhaps the best string notation we can use in JavaScript is JSON — JavaScript Object Notation. JSON is a lightweight data-interchange format inspired by JavaScript object literal notation as shown above. JSON is supported by PHP and many other server-side languages (refer to json.org).
There are two JSON methods in JavaScript:
Unfortunately, very few browsers provide these methods. To date, only Firefox 3.5, Internet Explorer 8.0 and Chrome 3 beta offer native support. Some JavaScript libraries offer their own JSON tools (such as YUI) but many do not (including jQuery).
However, all is not lost — JavaScript is flexible and we can implement the JSON stringify and parse methods whenever a browser requires them.
At the top of our code, we will create a JSON variable that points to the native JSON object or an empty object if it is unavailable:
The JSON.stringify code is a little more complex:
If JSON.stringify is not available, we define a new function that accepts a single obj parameter. The parameter can be a single value, an array, or a complex object such as obj1 above.
At Sportsbook your betting options go far beyond sports with a extensive menu of North American and International horse racing available daily as well as casino games and poker.Telephone wagering is not accepted. Mvp games online play.
The code examines the object type. Single values are returned immediately and only strings are modified to put quotes around the value.
If an array or object is passed, the code iterates through every property:
If your brain’s aching, you’ll be pleased to know that the JSON.parse code is much simpler:
/ / ) ) V / '/ / /. Freepbx system recordings.
This converts a JSON string to an object using eval().
Before you rush off to implement JSON serialization functions in all your projects, there are a few gotchas:
I hope you find the code useful. Feel free to use it in your own projects.
Resource files:
Related reading:
Coming soon: a useful application of JSON serialization…
Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |