"Turn" from:http://blog.csdn.net/semanwmj/article/details/7066306
JavaScript is always used for Web pages, and JSON is always used with JavaScript. If you use it recently, write it.
Here is a summary:
1. Convert the array in JavaScript and {} into JSON string can use Json2.js, source address Https://github.com/douglascrockford/JSON-js.
2. To convert a JSON string to a JavaScript object, you can use the Eval function that comes with JavaScript.
You can use TypeOf to view the types of variables in 3.javascript.
4. To access the JSON field, it must be access to the object.
jquery Instance Code:
1<script type= "Text/javascript" >2$(function(){ 3 varmyarray=NewArray ();///Array 4myarray[0]= "FGH"; 5myarray[1]= "RT"; 6myarray[2]= "XC"; 7 varArrstring=json.stringify (MyArray);//Convert a MyArray object to a string 8alert (arrstring);//["FGH", "RT", "XC"] 9 varObj1=eval ("(" +arrstring+ ")");///eval The string to a JSON object, note that the "(" and ")" are added on both sides TenAlert ("1th element of the array" +obj1[0]); One A varMyMap = {};//Object -mymap[' name '] = "LXD"; -Mymap[' age '] = 20; themymap[' content '] =MyArray; - varMapstring=json.stringify (MYMAP);///Convert the Mymap object to a string -alert (mapstring);///{"name": "Lxd", "age": +, "content": ["FGH", "RT", "XC"]} - varObj2=eval ("(" +mapstring+ ")"); +Alert ("Name field in Map:" +obj2.name); - }); +</script>
Why eval ("(" +mapstring+ ")")
The question of Eval itself. Since JSON may start and end in the way "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.
The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement).
Reference:
Http://www.cnblogs.com/myjavawork/archive/2011/03/10/1979279.html
Http://www.cnblogs.com/lidabo/archive/2011/12/29/2305770.html
http://www.sojson.com/
Array (array), {} (map) and JSON parsing in JavaScript