JQuery1.4.2 is compatible with the json format of earlier versions. _ jquery-js tutorial

Source: Internet
Author: User
The original code written with jQuery1.3.2 is replaced with 1.4.2, And the json file loaded with jQuery. ajax () cannot be loaded normally. (The same applies to jQuery. getJSON) The content of the original json file is:
{
Label: 'Europe (EU27 )',
Data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]
}
Solution 1:
To be changed to the standard json format, you must use "" For all strings. The modified content is as follows:
{
"Label": "Europe (EU27 )",
"Data": [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]
}
In this way, it can be loaded normally.
Solution 2:
Find "parseJSON: function" in the jQuery-1.4.2.js and you can find the following code:

The Code is as follows:


// Logic borrowed from http://json.org/json2.js
If (/^ [\], :{}\ s] * $/. test (data. replace (/\\(? : ["\\\/Bfnrt] | u [0-9a-fA-F] {4})/g ,"@")
. Replace (/"[^" \ n \ r] * "| true | false | null | -? \ D + (? : \. \ D *)? (? : [EE] [+ \-]? \ D + )? /G, "]")
. Replace (/(? : ^ |: | ,)(? : \ S * \ [) +/g ,""))){
// Try to use the native JSON parser first
Return window. JSON & window. JSON. parse?
Window. JSON. parse (data ):
(New Function ("return" + data ))();
} Else {
JQuery. error ("Invalid JSON:" + data );
}


The parseJSON function is used in httpData: function:

The Code is as follows:


// Get the JavaScript object, if JSON is used.
If (type = "json" |! Type & ct. indexOf ("json")> = 0 ){
Data = jQuery. parseJSON (data );


In jQuery1.3.2, the following code is directly used instead of parseJSON.

The Code is as follows:


// Get the JavaScript object, if JSON is used.
If (type = "json ")
Data = window ["eval"] ("(" + data + ")");


Replace it with the original 1.3.2 code.
Below are some other comments:
Jquery1.4.2 doubles the performance, but one headache is that $. the getJSON function. If the old version of JSON data is incorrectly written, JSON data cannot be obtained normally using this version.
For example:
Non-standard JSON format

The Code is as follows:


{Err: 1, errmsg: 'invalid ID value! Please submit from the correct table single page! '}


Earlier versions of jquery1.4.x can be obtained normally. If you use this type of format during program development, it is a headache, because if you upgrade JQUERY to the new version, this format cannot be read.
Standard JSON format. All versions can be obtained normally.

The Code is as follows:


{"Err": 1, "errmsg": "invalid ID value! Please submit from the correct table single page! "}


This is because native json parser is used in jquery1.4.X, which has strict requirements on the json format.
If you do not want to modify the JSON data of the program, what other methods can you use to make old data suitable for new versions?
There are some methods. You only need to restore the old version of the JSON processing function. The modification method is as follows:
Jq1.4.x Regular version Modification
Open the jquery-1.4.x.js file and find the following code:
Data = jQuery. parseJSON (data );
Modify to the following code:
Data = window ["eval"] ("(" + data + ")");
Jq1.4.x Minified version Modification
Open the jquery-1.4.x.min.js file and find the following code:
A = c. parseJSON ();
Modify to the following code:
A = window ["eval"] ("(" + a + ")");
Try your program, huh, huh, $. getJSON is normal?
Of course, if you have the ability to write regular expressions, you can modify the JSON processing regular expressions in the new parseJSON function.
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.