Use Jquery Json parsing basics

Foreword

In the WEB data transmission, JSON as text, i.e., a string passed lightweight, and the client is typically operated with a JS received JSON object, between each other so that, JSON object and JSON string conversion, parsing JSON data is the key.

First clear two concepts such as:

JSON string:

var str1 = '{ "name": "deyuyi", "sex": "man" }';

JSON object:

var str2 = { "name": "deluyi", "sex": "man" };

 

It can be simply understood:

JSON format objects can be used directly JQuery operations, such as may be used in C # object (class name) point out the property (Method) the same;

JSON string is just a string, as a whole, is not taken, then no way to remove the data stored therein, can not directly use, unless you just want to alert (him);

 

A, JSON string into a JSON object

 

To use the above str1, you use the following method to be converted to JSON object:

 

A: eval function

eval function essentially in line with or can be directly converted approximately conforming string JSON format as JSON object, using means such as:

the eval ( '(' str + + ')') ; // str which is a character string described in the title satisfied

Copy the code

    // string into a JSON object JSON
    var str='{ "name": "John" }';
    var obj = Avl ( '(' + Str + ')'); 
    alert( obj.name);

    var str2="{ 'name': 'John' }";
    was Obj2 = eval ( '(' + str2 + ')'); 
    alert( obj2.name);

    var str3="{ name: 'John' }";
    was obj3 = eval ( '(' + str3 + ')'); 
    alert( obj3.name);

Copy the code

Above will output "john".

Eval embodiment can convert the standard and non-standard format strings:

Copy the code

   var str="{ 'name': 'John' }";
   var str2='{ "name": "John" }';
   var str3="{ name: 'John' }";

Copy the code

Referring to the present embodiment download package: JqueryDemo1.html

 

B: parseJSON function

Another standard string into JSON object function is parseJSON (), using means such as jQuery.parseJSON (str) // str which is a string satisfies the title description

Copy the code

    // string into a JSON object JSON
    var str='{ "name": "John" }';
    var obj = jQuery.parseJSON (str)
    alert("1"+ obj.name);

Copy the code

Above will output "john".

This embodiment only supports standard formats: var str = '{ "name": "John"}';

Referring to the present embodiment download package: JqueryDemo2.html

 

C: JSON.parse function

There is also a standard string into JSON object function is JSON.parse (), using means such as the JSON.parse (str) // str which is a string satisfies the title description

Copy the code

        var str = '{ "name": "mady", "age": "24" }';
        var obj = JSON.parse (p);
        alert(obj.name);

Copy the code

Above will output "john".

This embodiment only supports standard formats: var str = '{ "name": "John"}';

Referring to the present embodiment download package: JqueryDemo3.html

 

Consistent with the above results, the names are output, as shown below:

clip_image002

 

Special Note: If obj has always been a JSON object, then use eval () function after conversion (even multiple conversions) or JSON object, but using parseJSON () function will have problems after treatment (grammar throws an exception).

D: Other way

 

If you can not help but want to make mistakes, very very much want to resolve non-standard, non-formal string, such as:

      {name:mady,age:23}

or

      {name:’mady’,age:23}

And the other you can think of all kinds of illegal nature of the right format, so there are extensions can be resolved

jquery-json extension library

Download here: http://code.google.com/p/jquery-json/

The library used to extend jQuery, JSON for use of extended two functions: toJSON and parseJSON

toJSON  function is used to sequence a regular JavaScript object as JSON objects.

parseJSON function is used to sequence a regular JavaScript object to JSON objects too.

Copy the code

      var data = $ toJSON ({x: 2, and 3}).; 
      var obj = jQuery.parseJSON (data); 
      alert(obj.x); 
      var str = {plugin: 'jquery-json', version: 2.3}; 
      was data2 = $. toJSON (str); 
      was obj2 = jQuery.parseJSON (Data2); 
      alert(obj2.plugin);

Copy the code

The above code execution results:

imageclip_image004

Referring to the present embodiment download package: JqueryDemo5.html

 

Second, the object is to convert a string JSON

You can use the toJSONString () or a global method the JSON.stringify () the object into JSON JSON string.

E.g:

var last = obj.toJSONString (); // JSON object into the character JSON

or

var last = JSON.stringify (obj); // JSON object into the character JSON

alert(last);

Third, read JSON parsing

We have a variety of ways to convert a string JSON object is to interpret him.

As the above example:

      var str2 = { "name": "mady", "sex": "man" };

It can be read this way:

      alert (str2.name); // C # and Wang Chu as a direct point ...

Pop-up "mady".

JSON we encountered rarely so simple, like a little complex JSON object, such as:

      var str={"GetUserPostByIdResult":{"Age":"33","ID":"2server","Name":"mady"}};

Resolved with:

      alert (str.GetUserPostByIdResult.Name); // a point does not come out, I several times more

Pop: "mady".

Amassing more complex, such as:

Copy the code

      var data=" { root: [ {'name':'6200','value':'0'}, {'name':'6101','value':'xa'}, {'name':'6102','value':'beijing'}, {'name':'6103','value':'haerbin'}]}";

Copy the code

If you want singled, then resolved with:

      alert(dataObj.root[0].name);

Pop: "6200."

If you want to pick the group, then parsed with:

Copy the code

      $.each(dataObj.root, function(index, item) {
            $("#info").append(
                    "<div>" +index+":"+ item.name + "</div>" + 
                    "<div>" +index+":"+ item.value + "</div><hr/>");
        });

Copy the code

Where the "#info" is a DIV's ID. Enter the results as shown below:

 

clip_image006

Referring to the present embodiment download package: JqueryDemo4.html

Note: This Example If another change within the string conversion function single quotes double quotation, quotation outer single quotes.

All the code in this article: Download

 

Original articles published 0 · won praise 0 · Views 47

Guess you like

Origin blog.csdn.net/wchqzhy99/article/details/104046305