How to Use Json in jQuery

Source: Internet
Author: User
Next, I will introduce several examples to you. I believe that all the friends who work on the web page will encounter n-level menus, but when you click a menu button, if its sub-menus are displayed, the sub-menu is hidden. If the sub-menu is hidden, it is displayed. The traditional javascript method is to first use getElementById to retrieve the id of the container where the sub-menu is located.

Next, I will introduce several examples to you. I believe that all the friends who work on the web page will encounter n-level menus, but when you click a menu button, if its sub-menus are displayed, the sub-menu is hidden. If the sub-menu is hidden, it is displayed. The traditional javascript method is to first use getElementById to retrieve the id of the container where the sub-menu is located, and determine the extension of Jquery's Json

If the value is equal to, it is set to block. If the value is not equal to, it is set to none. If the effect is more complex, the submenu is not suddenly hidden or displayed when you click the button, it is a smooth change of height. In this case, you need to set the height of the sub-menu through setTimeout.

A little more complex transparency also disappears and becomes apparent. At this time, a lot of code needs to be written. If the js foundation is poor, you may only need to get the code written by others and modify it! JQuery only needs one sentence to implement the above results. $ ("# a"). toggle ("slow"), do I need to copy and modify others' code after learning jQuery? Next we will introduce jQuery's methods for effect processing one by one.

This article expands the json string processing capability on the basis of jQuery. Great jQuery will be more efficient in writing javascript with jQuery, and jQuery's ajax encapsulation is also in place; after using jQuery for a while, we found that jQuery is not very powerful in processing json strings, at least compared with prototype. js is far behind.

Previously, I used to extend Jquery's Json. It was very convenient to process json data during ajax development. In jQuery, only one simple jQuery is provided. the getJSON () method does not provide jQuery's own processing of json data conversion. It is easier to convert a json string to a javascript Data Object using the eval () function, however, it is difficult to convert the javascript data type into a json string.
Use prototype. js and jQuery.

Json extension of Jquery

// Extended jQuery's conversion of json strings

JQuery. extend ({

/*** @ See converts a json string to an object * @ param json string * @ return returns objects such as object, array, and string */

EvalJSON: function (strJson ){

Return eval ("(" + strJson + ")");

}

});

JQuery. extend ({

/*** @ See converts the javascript data type to a json string * @ param: the object to be converted. It supports object, array, string, function, number, boolean, regexp * @ return returns a json string */

ToJSON: function (object ){

Var type = typeof object;

If ('object' = type ){

If (Array = object. constructor) type = 'array ';

Else if (RegExp = object. constructor) type = 'regexp ';

Else type = 'object ';

}

Switch (type ){

Case 'undefined ':

Case 'unknown ':

Return;

Break;

Case 'function ':

Case 'boolean ':

Case 'regexp ':

Return object. toString ();

Break;

Case 'number ':

Return isFinite (object )? Object. toString (): 'null ';

Break;

Case 'string ':

Return '"' + object. replace (/(\ | \ ")/g," \ $1 "). replace (/\ n | \ r | \ t/g, function (){

Var a = arguments [0];

Return (a = '\ n ')? '\ N': (a =' \ R ')? '\ R': (a =' \ t ')? '\ T ':""

}) + '"';

Break;

Case 'object ':

If (object = null) return 'null ';

Var results = [];

For (var property in object ){

Var value = jQuery. toJSON (object [property]);

If (value! = Undefined) results. push (jQuery. toJSON (property) + ':' + value );

}

Return '{' + results. join (',') + '}';

Break;

Case 'array ':

Var results = [];

For (var I = 0; I< Object. length; I ++ ){

Var value = jQuery. toJSON (object [I]);

If (value! = Undefined) results. push (value );

}

Return '[' + results. join (',') + ']';

Break;

}

}

}); Example:

Var obj = {

Name: "sean ",

Friend: ["fans", "bruce", "wawa"],

Action: function (){

Alert ("gogogog ")

},

Boy: true,

Age: 26,

Reg:/\ B ([a-z] +) \ 1 \ B/gi,

Child :{

Name: "none ",

Age:-1

}

};

Var json = $. toJSON (obj );

Var objx = $. evalJSON (json );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.