IE6 IE7 IE8 (Q) JSON objects are not supported

Source: Internet
Author: User
Tags rfc knowledge base

Standard reference

JSON is a data interchange format that is described in detail in RFC 4627.

As described in version 5th of ECMA-262 (ECMAScript), JSON is a simple object that contains the functions parse and stringify. The parse function is used to parse a JSON literal (a JSON-formatted string) into a ECMAScript value (for example, a JSON object is parsed into an ECMAScript object, the JSON array is parsed into an ECMAScript array, other types, etc.); s Tringify instead, it parses a ECMAScript value into a JSON-formatted string, such as a string that resolves a ECMAScript object to a JSON object.

For more information on JSON, refer to RFC 4627 and http://json.org.

For more information on JSON objects, refer to 15.12 the JSON object in ECMA-262 5th Edition.

Problem description

IE6 IE7 IE8 (Q) does not support JSON objects.

The impact

Scripting code that uses a JSON object may throw an exception when run in IE6 IE7 IE8 (Q), causing the function to fail.

The affected browser
IE6 IE7 IE8 (Q)
Problem analysis

The JSON object was implemented in the 5th edition of ECMAScript, which was released in December 2009 and IE6 IE7 and IE8 (q) (IE8 (q) equivalent to IE 5.5) were released earlier and did not implement the object in its Javascript engine.

Analyze the following code:

<script type= "Text/javascript" >  window.onload = function () {    var info = document.getElementById ("info") ;    if (window. JSON) {      var jsonstr = ' {' "name": "W3help", "url": "www.w3help.org", "Tech": ["JS", "HTML", "CSS", 5, 4.01, 2.1], '          + ' "Online": True, "category": {"RCA": "Root cause artical", "KB": "Knowledge Base"}, '          + ' "Version": 1} ', p, w3help;      W3help = Json.parse (JSONSTR);      For (p in w3help)        info.innerhtml + = p + ":" + w3help[p] + "<br/>";      info.innerhtml + = json.stringify (w3help);    } else      info.innerhtml = "not support JSON object.";  } </script><div id= "Info" style= "width:350px;" ></div>

The above code first detects if a JSON object exists under window, and then simply tests the parse and stringify functions of the JSON object and outputs (deserializes) the result. Execute the code, which behaves as follows in each browser:

IE8 (S) Firefox Chrome Safari Opera IE6 IE7 IE8 (Q)
Solution Solutions

The JSON format string can be parsed using window.eval () or new Function () {}. Such as:

<script type= "Text/javascript" >  window.onload = function () {    var info = document.getElementById ("info") ,      jsonstr = ' {' "name": "W3help", "url": "www.w3help.org", "Tech": ["JS", "HTML", "CSS", 5, 4.01, 2.1], '          + ' "online" : True, "category": {"RCA": "Root cause artical", "KB": "Knowledge Base"}, '          + ' "Version": 1} ', p, w3help;    Use two simple ways to parse the JSON format string    json1 = eval ("(" + Jsonstr + ")"),    Json2 = (new Function ("return" + Jsonstr));    For (p in Json1)      info.innerhtml + = p + ":" + json1[p] + "<br/>";    info.innerhtml + = "-----------------------<br/>";    For (p in Json2)      info.innerhtml + = p + ":" + json2[p] + "<br/>";  } </script><div id= "Info" style= "width:350px;" ></div>

The browsers perform the following:

All browsers

The above code can be seen as a simple alternative to json.parse () cross-browser. The same json.stringify () can be achieved by judging the type plus the algorithm, which is not mentioned here.

It is important to note that this simple implementation of parsing JSON format strings has a security problem, and that the inserted malicious JSON string (such as obtaining the user's cookie information) may be parsed and executed. You can use some proven security-proven solutions, such as json.parse () or Jquery.parsejson () in Json2.js.

See Knowledgebase
    • ...
Related issues
    • SJ9006: In IE6 IE7 IE8 (Q) cannot add ', ' after the last key value of the JSON direct amount
Test environment
Operating system version: Windows 7 Ultimate Build 7600
Browser version: IE6
IE7
IE8
Firefox 3.6.10
Chrome 7.0.544.0 Dev
Safari 5.0.2 (7533.18.5)
Opera 10.62
Test page: Json.html
Json_solution.html
This article was updated when: 2010-10-11
Key words

JSON Parse Stringify

IE6 IE7 IE8 (Q) does not support JSON objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.