2013-03-22 69 views
2

不同的結果,我遇到麻煩旁,試圖用JSON.stringify與「替代品」。JSON.stringify(值,替代品)在Chrome和FF

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和螢火蟲的 'JSON.stringify(結果,檢查員(結果)));'不同的是 -

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}]]* 

可能有人解釋我,爲什麼它在這些瀏覽器不同,我怎麼可能修改所以它會像GH一樣在FF中工作? 順便說一句 - 「結果」本身就是兩個瀏覽器相同。

P.S.我用that question爲檢查員()創建

+0

[MDN(https://developer.mozilla.org/如果在轉換過程中遇到一個XML值,它可以被省略(當它在對象中被發現時)或被審查爲空(當在一個對象中被發現時)它是在一個數組中找到的)*「FF似乎把它當作只有非DOM屬性的對象 – Bergi 2013-03-22 12:53:09

+0

你在做什麼? DOM結果應該序列化爲(X)HTML,而不是JSON – Bergi 2013-03-22 12:54:56

+0

我在selenium客戶端(在PHP上)創建測試方法。我正在使用executeScript()來運行此腳本,並希望獲得JSON響應以將其解析爲PHP數組。 – 2013-03-22 13:38:07

回答

0

使用,其通過引用而不是通過值傳遞censor回調,然後簡化實現:

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

JSON.stringify(result, censor);