admin管理员组

文章数量:1355564

Some code that I don't have control over is overriding the global JSON object without checking if it's already implemented:

var JSON = {
  org: "",
  copyright: "(c)2005 JSON",
  license: ".html",
  stringify: function(a, g) {
     ...

The problem is that this version of the JSON parser is very old and has a bug, which is fouling up my attempts at serialization. (Others have had a similar problem with this implementation.)

Can I get at the browser's native implementation? I thought delete would work, but it doesn't. I suspect that's because JSON is an object and not a method in the prototype. Is there some other way to get at it?

Some code that I don't have control over is overriding the global JSON object without checking if it's already implemented:

var JSON = {
  org: "http://www.JSON",
  copyright: "(c)2005 JSON",
  license: "http://www.crockford./JSON/license.html",
  stringify: function(a, g) {
     ...

The problem is that this version of the JSON parser is very old and has a bug, which is fouling up my attempts at serialization. (Others have had a similar problem with this implementation.)

Can I get at the browser's native implementation? I thought delete would work, but it doesn't. I suspect that's because JSON is an object and not a method in the prototype. Is there some other way to get at it?

Share Improve this question edited Apr 4, 2017 at 8:34 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Jun 20, 2012 at 23:16 paleozogtpaleozogt 6,57311 gold badges54 silver badges98 bronze badges 15
  • 2 Similar questions have popped up recently. You can create an empty iframe and get the JSON object from it. Or included a better library afterwards. – Felix Kling Commented Jun 20, 2012 at 23:20
  • ohh, an empty iframe. That's very clever. Surely that's the answer? – paleozogt Commented Jun 20, 2012 at 23:23
  • I'm curious how you can get the information from the DOM of the iframe, I thought that the browser sandboxed the javascript of the child frames ... – jcolebrand Commented Jun 20, 2012 at 23:24
  • Well, this will of course only work in browsers which provide the JSON object. If you also have to supported older browsers, you definitely have to include another library. – Felix Kling Commented Jun 20, 2012 at 23:24
  • 2 @jcolebrand: If he can inject code before the other, then yes, it makes more sense. But this is not clear from the question. – Felix Kling Commented Jun 20, 2012 at 23:31
 |  Show 10 more ments

2 Answers 2

Reset to default 12

You can create an iframe element (which will load about:blank and hence create a new context) and get a JSON object from there.

function restoreJSON() {
  var f = document.createElement("iframe");
  f.style.display = "none";
  document.documentElement.appendChild(f);
  window.JSON = f.contentWindow.JSON;
  document.documentElement.removeChild(f);
}

about:blank is loaded synchronously, so no need to wait for the load event. While this isn't restoring the original JSON object, it is getting one black-box identical to it.

Since the code that you don't have control over is overriding the original before you e along in the page, you have two options:

Inject an iframe and grab the JSON off the contextWindow (as indicated in the other answer on this question at the time of this edit), or, alternately, just use the https://github./douglascrockford/JSON-js JSON library as your own insert. Note that using Crockford's does give cross-browser-guarantees of conformance, but the native implementations are often faster.

An alternative if you have the ability in the future to e along on the page before the offending code, is to inject something before that offending "code that helps" to grab the JSON object:

<html>
  <head>
    <script>
      window.myJson = window.JSON;
    </script>
 ....

本文标签: javascriptrestore overridden windowJSON objectStack Overflow