Browser-Native JSON Support (Window.JSON)

Browser-native JSON support (window.JSON)

All modern browsers support native JSON encoding/decoding (Internet Explorer 8+, Firefox 3.1+, Safari 4+, and Chrome 3+). Basically, JSON.parse(str) will parse the JSON string in str and return an object, and JSON.stringify(obj) will return the JSON representation of the object obj.

More details on the MDN article.

Will JSON Evolve to Have Native Support for Ecmascript Map Objects?

No, because JSON notation, while it originated with Javascript, is widely used in a very large number of languages, but a Javascript Map only has meaning within the context of Javascript.

Any sort of change to JSON notation to allow for the serialization and deserialization of objects that only have meaning in Javascript would make that JSON format incompatible with most other languages.

This isn't to say that a custom parser based on JSON couldn't be written to properly serialize and deserialize Maps, but such a thing would only be based on JSON, rather than being an addition to the JSON standard.

Is JSON.parse supported by all major browsers?

The answer in 2013 (and later)

Is JSON.parse supported by all major browsers?

Pretty much, yes (source). Even IE8 has it (provided you're not in IE7 emulation mode). If you need to support IE7 and earlier, read on.

The original answer from 2011

No, older browsers (IE7 for instance) mostly don't have it. (More: http://caniuse.com/#search=JSON.parse)

However, just a small script is all you need. The inventor of JSON, Douglas Crockford, has no fewer than three for you to choose from on his Github page:

  • json2.js: Provides both JSON.parse and JSON.stringify. Parsing uses a few regexes to defend against script injection attacks and then passes the result to eval. This isn't generally considered a very good idea.
  • json_parse.js: A recursive-descent parser that doesn't use eval.
  • json_parse_state.js: A state-machine parser that doesn't use eval.

Use what suits you. :-)

Just about any major library (like jQuery, Prototype, YUI, Closure, or any of several others) will also provide JSON parsing, although in some cases it may well be a thin veneer on eval.

restore overridden window.JSON object

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.

How Supported is JSON

The response is parsed because jQuery recognizes it is JSON based on the Content-Type reported by the server. Native browser support for parsing JSON is universal at this point, but jQuery 1.x does include its own implementation as a fallback.

Therefore:

  1. If you write your own AJAX library and want to have automatic parsing you will have to code the triggering logic yourself. Unless you care about ancient browsers the parsing is built-in (JSON.parse).
  2. If you use jQuery you don't need to worry about anything as long as the server sends the correct internet media type.
  3. Even if the server is buggy you can instruct jQuery to parse the response as JSON (the dataType option).

Access native JSON object when JSON2 has overloaded it

not sure if i understand your problem correctly

if you place an empty iframe like this

<iframe id="testFrame" name="testFrame" src="about:blank" style="display:none;"></iframe>

then you can also call from js

testFrame.JSON.stringify(obj);

the only problem is that if you use it in https: src could be javascript:false if you need to support IE6

EDIT: I still think i don't deserve the answer being accepted, so i've come up with a modified version of your code

(function($) {
var frm;
$.getNative = function(objectName, callback) {
if (!frm) {
frm= $("<iframe>", {
src: "javascript:false",
style: "display:none;"
}).appendTo("body").load(function(){
callback(this.contentWindow[objectName]);
// $(this).remove(); <-- this is commented to cache the iframe
});
}
callback(frm[0].contentWindow[objectName]);
}
})(jQuery)

this will enable you to use $.getNative() multiple times in a document without recreating the frame each time.

How to parse a json string alert

Most modern browsers support the JSON object:

var errorObject = JSON.parse(ff.Result.value);
alert(errorObject.data[0]);

See Browser-native JSON support (window.JSON)

An example using json2.js:

<script src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
<script>
var errorObject = JSON.parse(ff.Result.value);
document.getElementById('someId').innerHTML += errorObject.data[0];
</script>

Serializing an object to JSON

You're looking for JSON.stringify().

Need to iterate through stored json objects in local storage array of json data

If you have a string representation of a JSON object, one way to turn it into a javascript object to iterate through is:

eval(offlinExtractData)[3]

In IE 8+, Firefox 3.1+, Safari 4+, Chrome 3+, and Opera 10.5+ (based on Browser-native JSON support (window.JSON)) you can also avoid eval by using:

JSON.parse(offlinExtractData)[3]

For even more support, you could also use an external JSON parser such as https://github.com/douglascrockford/JSON-js/blob/master/json2.js



Related Topics



Leave a reply



Submit