zoukankan      html  css  js  c++  java
  • jQuery1.4.2与json格式兼容问题 (转载)

    原来使用jQuery1.3.2编写的代码,更换到1.4.2后,使用jQuery.ajax()加载的json文件,不能正常加载。(使用jQuery.getJSON()也一样)

    原json文件内容为:

    {
        label: 'Europe (EU27)',
        data: [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
    }

    解决方法一:

    改成标准的json格式,要求对字符串都使用""限定,修改后的内容为:

    {
        "label": "Europe (EU27)",
        "data": [[1999, 3.0], [2000, 3.9], [2001, 2.0], [2002, 1.2], [2003, 1.3], [2004, 2.5], [2005, 2.0], [2006, 3.1], [2007, 2.9], [2008, 0.9]]
    }

    这样就可以正常加载了。

    解决方法二:

    在jQuery-1.4.2.js中找到"parseJSON: function",可发现有如下代码:

    Js代码 复制代码
    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.   
    6.   
    7.     // Try to use the native JSON parser first   
    8.     return window.JSON && window.JSON.parse ?   
    9.         window.JSON.parse( data ) :   
    10.         (new Function("return " + data))();   
    11.   
    12. } else {   
    13.     jQuery.error( "Invalid JSON: " + data );   
    14. }  
    				// 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 ); 		}


    在httpData: function中用到了parseJSON函数:

    Js代码 复制代码
    1. // Get the JavaScript object, if JSON is used.   
    2.             if ( type === "json" || !type && ct.indexOf("json") >= 0 ) {   
    3.                 data = jQuery.parseJSON( data );  
    // Get the JavaScript object, if JSON is used. 			if ( type === "json" || !type && ct.indexOf("json") >= 0 ) { 				data = jQuery.parseJSON( data );
     

    在jQuery1.3.2中,没有parseJSON这个方法,而是直接使用下面的代码。

    Js代码 复制代码
    1. // Get the JavaScript object, if JSON is used.   
    2.             if ( type == "json" )   
    3.                 data = window["eval"]("(" + data + ")");  
    // Get the JavaScript object, if JSON is used.             if ( type == "json" )                 data = window["eval"]("(" + data + ")");

    替换成原来1.3.2的代码就可以了。

     转自:http://cleaneyes.javaeye.com/blog/663883

  • 相关阅读:
    JavaScript之数学对象Math
    JavaScript之数据类型转换
    JavaScript之操作符
    JavaScript之基本语句
    JavaScript之基本概念(二)
    JavaScript之基本概念(一)
    使用velero进行kubernetes灾备
    minikube配置CRI-O作为runtime并指定flannel插件
    使用thanos管理Prometheus持久化数据
    linux开启tcp_timestamps和tcp_tw_recycle引发的问题研究
  • 原文地址:https://www.cnblogs.com/johnwonder/p/1776196.html
Copyright © 2011-2022 走看看