Array (array), {} (map) and JSON parsing in JavaScript

Source: Internet
Author: User

"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

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.