Today's Question:  What does your personal desk look like?        GIVE A SHOUT

JSON in JavaScript

  sonic0002        2013-05-04 23:25:57       5,268        0    

When sending an AJAX request to the server, the response can have two formats : XMLHttpRequest.responseXML to access data with XML format and XMLHttpRequest.responseText to access data with string format. XML is the standard data transfer format, but one weakness is it's troublesome to parse and retrieve the data.

JSON(JavaScript Object Notation) is a light weight data interchange format, we call it the JavaScript object representation. The advantage of using JSON as the data format is itself is JavaScript. We can first get the data from server with XMLHttpRequest.responseText and then using JavaScript's eval() function to convert the JSON string to a JavaScript object.

In addition there are many libraries which support JSON in other languages such as C++,C#,ColdFusion,Java,Perl,PHP and Python.

1. JSON syntax

JSON consists of two structures:

Object -- name/value pairs, in different languages,it is understood as object,record,structure,dictionary,hash table,keyed list or relational array. One object starts with left brace"{", ends with right brace"}". Each name is followed by a colon":",name/value pairs are separated with coma",".

Array -- A value list, in most languages, it is understood as an array which starts with "[" and ends with "]". The values are separated with ",".

JSON doesn't have variables and other control statements. It only contains data.

a). Data type

JSON data structure consists different data types : string,number,boolean,null,object,array

JSON strings must be enclosed with double quote. They can contain escaped characters such as \",\b,\n,\f,\r,\t,\uXXXX,\\,\/.

b). JSON structure

JSON can be expressed as object literal, if there is more than one member object, then we should express it as an array.

 {“memeber”:[
      {
                  “name”:”Tom”,
                  “age”:22,
                  “country”:”USA”
      },
      {
                  “name”:”WangMing”,
                  “age”:25,
                  “country”:”China”
      }
      ]
 }

c). JSON parser

In ECMAScript 262 standard, a JSON object is defined which have two methods: JSON.parse() and JSON.stringify().JSON.parse() parses a JSON string to JavaScript object and JSON.stringify() converts a JavaScript object to JSON string.

2. Create XMLHttpRequest to get JSON data

a). Create request

If we want to directly request the JSON data in a JSON file on server, we can run:

request.open(“GET”,”classes.txt”,true);

Here classes.txt is the JSON filename

b). Parse the response

We can use either eva(0 or JSON.parse() to convert the JSON string received to a JavaScript object.

With eval():

 var jsonResp=request.responseText;
 jsonResp=eval(“(”+jsonResp+”)”);

Here we include the string in a () because sometimes the string cannot be correctly evaluated.

With JSON.parse();

 var jsonResp=request.responseText;
 jsonResp=JSON.parse(jsonResp);

The JSON.parse() method is more secure since it will escape the string passed.

JSON now is a part of JavaScript standard and all modern browsers support parse of JSON data. So when we create AJAX request, we recommend you to use JSON, it's light weight and simple to use. Many APIs are supporting JSON as well.

Reference : http://www.yiiyaa.net/1161

JAVASCRIPT  JSON 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.