【问题标题】:JSON.stringify(value , replacer) different results in Chrome and FFJSON.stringify(value, replacer) 在 Chrome 和 FF 中的不同结果
【发布时间】:2013-03-22 11:57:27
【问题描述】:

我遇到了下一个麻烦,尝试将 JSON.stringify 与“替换器”一起使用。

var scriptTag = document.createElement('script');
        scriptTag.type = 'text/javascript';
        scriptTag.src = 'https://wicked-good-xpath.googlecode.com/files/wgxpath.install.js';
        document.body.appendChild(scriptTag);


function censor(censor) {
  var i = 0;

  return function(key, value) {

console.log(i,typeof(censor),'=====',typeof(value), value);    
    if(i !== 0 && /*typeof(censor) === 'object' && typeof(value) == 'object' && */ censor == value) 
      return null; 
console.log(i,typeof(censor),'=====',typeof(value), value); 
    if(i >= 29) // seems to be a harded maximum of 30 serialized objects?
      return null;
console.log(i,typeof(censor),'=====',typeof(value), value); 
    ++i; // so we know we aren't using the original object anymore

    return value;  
  };
}

XPathResult = document.evaluate('**<SOME XPATH HERE>**', document, null, XPathResult.ANY_TYPE, null);
var actualNode = XPathResult.iterateNext();
                    var result = [];
                    while (actualNode) {
                        result.push(jQuery.makeArray(actualNode));
                        actualNode = XPathResult.iterateNext();
                    }
console.log(result);                
console.log("Result: ", JSON.stringify(result, censor(result)));

pastebin

问题在于 DevTools 和 Firebug 中的 'JSON.stringify(result, censor(result)));'不一样——

Chrome 输出:

console output of **30** objects

Result:  [[{"width":"","vAlign":"","scope":"col","rowSpan":1,"noWrap":false,"height":"","headers":"","colSpan":1,"chOff":"","ch":"","bgColor":"","axis":"","align":"","abbr":"","cellIndex":4,"spellcheck":true,"isContentEditable":false,"contentEditable":"inherit","children":{"length":0},"outerText":"PUBLICATION DATE","outerHTML":"<th scope=\"col\" class=\"sortDesc byPublicationDate\" style=\"cursor: pointer;\">PUBLICATION DATE</th>","innerText":"PUBLICATION DATE","innerHTML":"PUBLICATION DATE","accessKey":"","hidden":false,"webkitdropzone":null,"draggable":null,"tabIndex":null,"dir":null,"translate":null,"lang":null,"title":null,"id":null,"webkitShadowRoot":null,"webkitPseudo":null,"childElementCount":null,"nextElementSibling":null,"previousElementSibling":null,"lastElementChild":null,"firstElementChild":null,"dataset":null,"classList":null,"className":null,"scrollHeight":null,"scrollWidth":null,"scrollTop":null,"scrollLeft":null,"clientHeight":null,"clientWidth":null,"clientTop":null,"clientLeft":null,"offsetParent":null,"offsetHeight":null,"offsetWidth":null,"offsetTop":null,"offsetLeft":null,"style":null,"tagName":null,"parentElement":null,"textContent":null,"baseURI":null,"localName":null,"prefix":null,"namespaceURI":null,"ownerDocument":null,"attributes":null,"nextSibling":null,"previousSibling":null,"lastChild":null,"firstChild":null,"childNodes":null,"parentNode":null,"nodeType":null,"nodeValue":null,"nodeName":null,"jQuery17109208346924278885":null}]]

和FF输出:

*console output of **3** objects

Result: 
[[{"jQuery171005647180282625541":13}]]*

有人可以解释一下,为什么它在这些浏览器中的工作方式不同,我该如何修改它以使其在 FF 中与 GH 一样工作? 顺便说一句 - “结果”本身在两个浏览器中都是相同的。

附:我使用that question 进行 censor() 创建

【问题讨论】:

  • MDN 说:“如果 [...] 在转换过程中遇到 XML 值,它要么被忽略(当它在对象中找到),要么被审查为 null(当它被发现在一个数组中)" FF 似乎把它当作一个只有非 DOM 属性的对象
  • 你想做什么? DOM 结果应序列化为 (X)HTML,而不是 JSON
  • 我正在 selenium 客户端(在 PHP 上)中创建测试方法。我正在使用 executeScript() 运行此脚本,并希望获取 JSON 响应以将其解析为 PHP 数组。

标签: javascript json xpath


【解决方案1】:

使用通过引用而不是通过值传递censor 的回调,然后简化实现:

var censor = "";
function censorRun(key, value)
   {
    if(censor == value) 
      return null; 
    else
      return value;  
   }

JSON.stringify(result, censor);

【讨论】:

    猜你喜欢
    • 2017-07-01
    • 2023-03-19
    • 2016-05-16
    • 1970-01-01
    • 2011-11-05
    • 2022-08-17
    • 1970-01-01
    • 2012-12-10
    • 2011-08-23
    相关资源
    最近更新 更多