JSON (02) JSON parsing and Application in JQuery

Source: Internet
Author: User

When JQuery is used for frontend page interaction with background programs, JSON plays a major role in recording JQuery's related things.


Several functions related to JSON processing:
1. convert a string to a JSON object
A. JavaScript is converted using eval, but this method is not secure and is not recommended.
Var jsonObj =Eval(JsonStr );

B. JSON. parse mode, but ie6 and ie7 are not supported. You need to download a JS extension at http://www.json.org/js.html.
Var jsonObj =JSON. parse(JsonStr );

C. JQuery uses parseJSON functions to improve security
Var jsonObj =JQuery. parseJSON(JsonStr );

2. Convert the JSON object into a string
Var jsonStr =JSON. stringify(JsonObj );

3. getJSON (). This function obtains json data through the get request.
JQuery. getJSON (url, data, callback)
· Url is used to provide the address page of json data
Data (Optional) is a key-value pair that is sent to the server.
· Callback (Optional) callback function, the processing function after the json data request is successful
For details, refer:
Http://www.w3school.com.cn/jquery/ajax_getjson.asp
4. GET and. POST can both directly transmit or receive JSON strings.
....


JSON display:

Example 1.

[{"Ret": "c:/tmp/11gR2", "type": "DIR" },{ "ret": "c:/tmp/xcl. log "," type ":" FILE "},{" ret ":" c:/tmp/soft "," type ":" DIR "}]

The expanded view is as follows:

{"Name": "xcl. log", "type": "FILE", "fileversion ":[
{& Quot; version & quot;: 1, & quot; backupTime & quot;: 100, & quot; createtime & quot;: 80, & quot; modifytime & quot;: 70, & quot; size & quot;: & quot; 10 m & quot "},
{& Quot; version & quot;: 2, & quot; backupTime & quot;: 120, & quot; createtime & quot;: 50, & quot; modifytime & quot;: 80, & quot; size & quot;: & quot; 50 m & quot;}]}

Here is an example of JSON usage:

 JSON DEMO<Script src = "http://code.jquery.com/jquery-1.11.0.min.js"> </script> <script type = "text/javascript"> $ (document ). ready (function () {function demo1 () {// string var jsonStr = '[{"ret": "c:/tmp/11gR2", "type ": "DIR" },{ "ret": "c:/tmp/xcl. log "," type ":" FILE "},{" ret ":" c:/tmp/soft "," type ":" DIR "}] '; // convert a string to a JSON object // JavaScript is converted using eval, but this method is not safe and is not recommended. // Var jsonObj = eval (jsonStr); // JSON. parse mode, but ie6 and ie7 are not supported. You need to download a JS extension at http://www.json.org/js.html. Var jsonObj = JSON. parse (jsonStr); // JQuery uses the parseJSON function to improve the security of var jsonObj = jQuery. parseJSON (jsonStr); // use each to traverse the JSON array var info = "length =" + jsonObj. length; $. each (jsonObj, function (n, value) {info + ="
"+ Value. type + ':' + value. ret}); ('{jsoninfo_div'{.html (info);} function demo2 () {// directly assign a complex JSON to the JS variable var jsonObj = {"name": "xcl. log "," type ":" FILE "," fileversion ": [{" version ": 1," backupTime ": 100," createtime ": 80," modifytime ": 70, "size": "10 m" },{ "version": 2, "backupTime": 120, "createtime": 50, "modifytime": 80, "size": "50 m"}]} var info = "name =" + jsonObj. name; info + ="
"+" Length = "+ jsonObj. fileversion. length; $. each (jsonObj. fileversion, function (n, value) {info + ="
Version: "+ jsonObj. fileversion [n]. version + ': size:' + jsonObj. fileversion [n]. size;}); ('{jsoninfo2_div'{.html (info); ('{jsondemo21_div'{.html (JSON. stringify (jsonObj);} demo1 (); demo2 () ;}); </script>JSON DEMO
Demo1:


Demo2:




Effect:

MAIL: xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168



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.