gpt4 book ai didi

jquery - JSON 解析器错误

转载 作者:行者123 更新时间:2023-12-01 06:40:26 24 4
gpt4 key购买 nike

我不断收到以下数据的 JSON 解析器错误:

[{"data":"Aerospace and Defense"},{"data":"Agriculture"},{"data":"Business Services"},{"data":"Chemicals"},{"data":"Construction"},{"data":"Consumer Goods and Services"},{"data":"Education"},{"data":"Electronics"},{"data":"Energy and Utilities"},{"data":"Environmental Services and Equipment"},{"data":"Financial Services"},{"data":"Food and Beverage"},{"data":"Healthcare Goods and Services"},{"data":"Industrial Goods and Services"},{"data":"Information Technology"},{"data":"Metals and Mining"},{"data":"Security Products and Services"},{"data":"Software"},{"data":"Telecom"},{"data":"Tranportation and Storage"}]

任何人都可以确定错误是什么吗?我最初使用的是“航空航天与国防”,并认为特殊字符可能是问题所在。但看起来不像。

我使用 ASP.NET MVC JSONResult 将其传递回 jQuery。 FireFox 和 IE 显示正确创建的数据,但 jquery 解析器抛出错误。我听说新的 jQuery 中的解析器已进行了重大修改。

任何帮助将不胜感激。

最佳答案

我觉得还不错。请参阅DEMO .

另一个演示使用此 JSON 字符串来响应实际的 jQuery AJAX 请求:

我真的看不出如何对 jQuery JSON 解析器进行重大修改。这是actual source of jQuery.parseJSON在 jQuery 1.5.1 中:

parseJSON: function( data ) {
if ( typeof data !== "string" || !data ) {
return null;
}

// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );

// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test(data.replace(rvalidescape, "@")
.replace(rvalidtokens, "]")
.replace(rvalidbraces, "")) ) {

// 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 JSON regexp :

// JSON RegExp
rvalidchars = /^[\],:{}\s]*$/,
rvalidescape = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,
rvalidtokens = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,
rvalidbraces = /(?:^|:|,)(?:\s*\[)+/g,

但它是last modified 2010 年 9 月,作者:John Resig。

您使用的是什么版本?

关于jquery - JSON 解析器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5130995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com