Jquery1.4.2 compatibility with JSON format

Source: Internet
Author: User

Article category: Web Front-end

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:

JS Code
  1. // Logic borrowed from http://json.org/json2.js
  2. If (/^ [\], :{}\ S] * $/. Test (data. Replace (/\\(? : ["\\\/Bfnrt] | U [0-9a-fa-f] {4})/g ,"@")
  3. . Replace (/"[^" \ n \ r] * "| true | false | null | -? \ D + (? : \. \ D *)? (? : [EE] [+ \-]? \ D + )? /G, "]")
  4. . Replace (/(? : ^ |: | ,)(? : \ S * \ [) +/g ,""))){
  5. // Try to use the native JSON parser first
  6. Return window. JSON & window. JSON. parse?
  7. Window. JSON. parse (data ):
  8. (New function ("return" + Data ))();
  9. } Else {
  10. Jquery. Error ("invalid JSON:" + data );
  11. }

 


The parsejson function is used in httpdata: function:

JS Code
  1. // Get the JavaScript Object, if JSON is used.
  2. If (type = "JSON" |! Type & CT. indexof ("JSON")> = 0 ){
  3. Data = jquery. parsejson (data );

 

In jquery1.3.2, the following code is directly used instead of parsejson.

JS Code
  1. // Get the JavaScript Object, if JSON is used.
  2. If (type = "JSON ")
  3. Data = Window ["eval"] ("(" + Data + ")");

Replace it with the original 1.3.2 code.

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.