简体   繁体   English

IE6中的嵌套JSON会导致问题

[英]Nested JSON in IE6 causes problems

I'm making a request to a JSON page using jQuery's $.getJSON method, and from the returned JSON i'm creating some HTML and putting it on to the page. 我正在使用jQuery的$.getJSON方法向JSON页面发出请求,然后从返回的JSON创建一些HTML并将其放到页面上。

The problems appears when I have a nested JSON object, i'll show you an example later. 当我有一个嵌套的JSON对象时,就会出现问题,我将在后面为您提供示例。

First off, if I make a request to my JSON page and return the following JSON, the function works just fine and i see a beautiful HTML element appear on the page: 首先,如果我向JSON页面发出请求并返回以下JSON,则该函数运行正常,并且我在页面上看到漂亮的HTML元素:

JSON: JSON:

({
     "variants": [
         {
             "variantId": "536",
             "title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
             "price": "£299.00"         
         }
     ]
})

This works fine, no errors. 这工作正常,没有错误。

However, as soon as i return the JSON below, the function does not work. 但是,一旦我返回下面的JSON,该功能将无法使用。

({
     "variants": [
         {
             "variantId": "536",
             "title": "Party Like a Rock Star for Two at the Metropolitan hotel, London ",
             "price": "£299.00",
             "blogs": [
                 {
                     "title": "Another test",
                     "author": "Sean",
                 },
                 {
                     "title": "This is a test",
                     "author": "Sean",
                 }
             ]
         }
     ]
})

As you can see, there are no characters in it that would cause it too break. 如您所见,其中没有字符会导致其太破损。 I've also tried renaming the fields, just by chance that "blogs", "title", or "author" were reserved words in JS (as i thought, no difference!) 我也尝试过重命名字段,只是偶然地发现“博客”,“标题”或“作者”是JS中的保留字(正如我所想,没有区别!)

To make sure it was not my way of processing the data that was causing a problem, i stuck an alert('Got here.'); 为了确保不是我处理导致问题的数据的方式,我贴了一个警报(“在这里知道”); as the first piece of code (see below) in my $.getJSON function, and that doesn't fire so I know it's not what i'm doing with the data that is causing an error. 作为$ .getJSON函数中的第一段代码(请参见下文),并且不会触发,所以我知道这不是我正在处理导致错误的数据。

$.getJSON('/ajax/cover_flow_detail.ashx?experienceId=' + arguments[0], function(d) {
        alert('Got here'); // doesn't fire ?

        // omitted for brevity.

}

Even weirder - this is only happening in IE6. 甚至更奇怪-这仅发生在IE6中。 IE7 & FF are fine. IE7&FF很好。

Any push in the right direction would be appreciated, i'm completely stumped! 任何朝着正确方向的推动将不胜感激,我完全感到沮丧!

Cheers, Sean 肖恩干杯

You have an error in your JSON — trailing commas in some of your object definitions. JSON中有错误-一些对象定义中的逗号结尾。

(This suggests you are generating your JSON using a template instead of a JSON library, this is a mistake.) (这建议您使用模板而不是JSON库来生成JSON,这是一个错误。)

IE is less forgiving of that error than other browsers. 与其他浏览器相比,IE对该错误的容忍度较低。

Internet Explorer is notorious for breaking on trailing commas . Internet Explorer以末尾的逗号中断而臭名昭著

var obj = {
     upper: 1,
     stage: 2,
};

Fails on IE, while other browsers ignore the trailing comma after the second element. 在IE上失败,而其他浏览器则忽略第二个元素后的尾部逗号。

Ironically, it should be erroring. 具有讽刺意味的是,它应该是错误的。 IE did it right. IE浏览器做对了。 Browsers shouldn't loosely parse JSON if the syntax isn't right. 如果语法不正确,浏览器不应松散地解析JSON。 The , should be treated like a ; 的,应视为; then. 然后。

I believe it is the parser, because in EXT JS, it is strict. 我相信它是解析器,因为在EXT JS中,它是严格的。

Remember when HTML tags that were poorly closed would be ignored by IE, and not Netscape. 请记住,当关闭不当的HTML标记将被IE而不是Netscape忽略时。 Interesting in the turn around. 转身有趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM