Tutorials

Parsing JSON String

Oct 28, 2008 insic 2 Comments

In previous post about JSON Requests and Responses we learn how to send JSON request to our server-side scripts and return a JSON response.

Now back on the client, after the server has done what it needs to do, the response is set in the responseText property of the XMLHttpRequest object. Once the readyState and status are set to 4 and 200, respectively, the JSON string can be saved and eval( ).

Here is the code that handles in getting the JSON string and ready to parse.

This function, parseResponse, checks the request object for its readyState and status to see if the response is complete, and then parses the responseText (the JSON string) to get the results from the server.

The response is now a JavaScript object, and the object can be walked, searched, or manipulated just like any other DOM object.

Parsing the JSON response object.

And that’s it. Now you know how easy to get the data you need.

About the author: insic

Subscribe in my RSS Feed for more updates on Web Design and Development related articles. Follow me on twitter or drop a message to my inbox.

  • Antony Kennedy

    “The response is now a JavaScript object, and the object can be walked, searched, or manipulated just like any other DOM object.”

    I’d have to disagree. Traversing and manipulating the DOM and DOM elements is not the same as a native JS object. They are inherently different things.

    property and {‘value’:’property’} are not the same – the second can be implicit in datatype, and only needs to be evaluated to be converted into a native object. The first needs to be parsed in a more complex manner.

    Enjoying your blog :)

  • Pingback: 網站製作學習誌 » [Web] 連結分享