TL;DR: Adding any non-built-in functions to Array.prototype AND Function.prototype will cause the IE8 native JSON parser to get a stack overflow when parsing any JSON that contains an array, but only when you also pass a reviver function into JSON.parse().

这开始是一个问题,但我回答了我自己最初的问题,所以现在我要问:有没有人能想出一种解决此IE8错误的方法,而不涉及删除所有修改Array.Prototype和Function.Prototype的JS库?

Original question:

I have about 13k of JSON data to parse. The structure of the data is an object with a single value that is a nested array.

{ 'value':[[ stuff ], [ more stuff], [ etc ]] }

I'm using json2.js, which defers to the browser native JSON.parse when available. I'm passing a reviver function into JSON.parse to handle dates properly. When IE8 is in IE7 emulation mode (which causes it to use the script-based json2.js parser) everything works fine. When IE8 is in IE8 mode (which causes it to use the browser-native JSON parser) it blows up with an "out of stack space" error. Firefox and Chrome, of course, work just fine with their browser-native JSON parsers.

I've narrowed it down to this: if I pass even a do-nothing reviver function into JSON.parse, the IE8 native parser gets the stack overflow. If I pass in no reviver function, the IE8 native parser works fine, except it doesn't parse dates correctly.

// no error:
JSON.parse(stuff);

// "out of stack space" error:
JSON.parse(stuff, function(key, val) { return val; });

I'm going to play with my JSON data, to see if less data or less nesting of the data can avoid the error, but I was wondering if anyone had seen this before, or had any other suggested work-arounds. IE8 is slow enough already, it would be a shame to disable native JSON for that browser because of this bug.

UPDATE: In other cases, with different JSON data, I'm getting a javascript error "$lineinfo is undefined" when I use the IE8 native parser with a reviver function, and no error if I use no reviver function. The string "$lineinfo" does not appear anywhere in any of my source code.

UPDATE 2: Actually, this problem seems to be caused by Prototype 1.6.0.3. I was unable to reproduce it in an isolated test page until I added in the Prototype library.

UPDATE 3:

原因是原型.js打破了IE8原生JSON解析器的做法是:向数组中添加任何非内置函数.原型和功能.prototype将导致IE8原生JSON解析器在解析任何包含数组的JSON时出现堆栈溢出,但仅当您还向JSON传递了一个恢复器函数时.parse().

原型库将函数添加到两个数组中.原型和功能.原型,但这同样适用于做同样事情的任何其他库.IE JSON解析器中的这个错误是通过Prototype和Extexpose 的,而不是通过jQueryexpose 的.我还没有测试过任何其他框架.

以下是该问题的完全独立再现.如果删除Function.Prototype行或Array.Prototype行,或者从JSON字符串中删除数组,则不会出现"超出堆栈空间"的错误.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
<script type="text/javascript">

Function.prototype.test1 = function() { };
Array.prototype.test2 = function() { };

window.onload = function()
{
    alert(JSON.parse('{ "foo": [1,2,3] }', function(k,v) { return v; }));
}

</script>
</head>
<body>

</body>
</html>

Json相关问答推荐

从Json响应中为需要每个值的Post请求提取多个值

将JSON输入子数组转换为字符串顺序列表输出

JSON:将项';S键/名称移动到属性中,并使用JQ将其转换为数组

通过在织女星简化图上裁剪来显示文本

无法使用Jolt变换在嵌套的JSON中提取值

(Kotlin)com.google.gson.internal.LinkedTreeMap无法转换为com.example.phonetest2.model.HallData

为什么 Django Rest API 序列化器没有正确序列化多对多字段

在 postgres 14 中将记录转换为所需的 json 格式

jq :遍历 json 并在键存在时检索集合

使用 jq 将非统一的 json 输出转换为汇总表

每次在 SoapUI 中发送请求时,将 JSON 响应的子 node 分配给项目变量

如何将 XML 转换为 PsCustomObject 以允许最终导出为 JSON?

JSON RPC - 什么是id?

Android JSON 库的性能和可用性比较

如何使用 LWP 发出 JSON POST 请求?

Jackson 没有使用 @JsonProperty 覆盖 Getter

ASP.NET Core API 仅返回列表的第一个结果

使用 application/json 优于 text/plain 的优势?

请求返回字节,我无法解码它们

有没有办法折叠 Postman 中的所有 json 字段