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, and the other types are This analogy); stringify 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
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(){ varinfo = document.getElementById ("info"); if(window. JSON) {varJsonstr = ' {' "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(pinchw3help) info.innerhtml+ = p + ":" + w3help[p] + "<br/>"; Info.innerhtml+=json.stringify (W3HELP); } Elseinfo.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(){ varinfo = 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; //parse JSON format strings in two simple waysJson1 = eval ("(" + Jsonstr + ")"), Json2= (NewFunction ("return" +jsonstr)) (); for(pinchjson1) info.innerhtml+ = p + ":" + json1[p] + "<br/>"; Info.innerhtml+ = "-----------------------<br/>"; for(pinchjson2) info.innerhtml+ = p + ":" + json2[p] + "<br/>"; }</script><div id= "Info" style= "width:350px;" ></div>
The browsers perform the following:
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. can use some proven security-proven solutions, such as json.parse () or Jquery.parsejson () in Json2.js.