Notes for converting text into JSON objects using JQuery _ jquery

Source: Internet
Author: User
Among many JQuery methods, many method parameters can be passed into a JSON object, such as the second parameter of the Ajax method. Note the following when converting text into a JSON object: 1) $. parseJSON returns a string instead of a JSON object.

2) to convert a string into an object, it is easy to think of the eval method in JS. In fact, this is acceptable, but parentheses must be added. For example, var js = "{\" PageIndex \ ": \" 1 \ "}"; var obj = eval ("(" + js + ")");. However, using eval is insecure because it can compile any js Code.

3) download a JSON parser because it only recognizes JSON text. This makes it safer. The official JSON website provides such a script. Address: http://www.json.org/json2.js. It is easy to use. After the file is introduced, for example, JSON. parse ($ ("# ctl00_ContentPlaceHolder1_hfSearch"). val (). toString ()).

4) use JSON. parse method or $. when using parseJSON, note that the name and value of JSON data are enclosed in double quotation marks. For $. the parseJSON method also enclose the JSON string in single quotes before conversion. For JSON. the parse method is no longer required. Really TMD is a waste of time.

Pay attention to these skills, so you can avoid a lot of detours when operating JSON data.
Related Article

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.