jquery to convert text into a JSON object a problem to note _jquery

Source: Internet
Author: User
Tags eval
1 The $.parsejson method returns a string instead of a JSON object.

2 to convert the string into an object, it is easy to think of the Eval method in JS. Actually, it's OK, but parentheses are needed. such as Var js= "{\" pageindex\ ": \" 1\ "}"; var obj=eval ("(" +js+) ");. Using eval, however, is unsafe because it can compile any JS code.

3) Download a JSON parser because it only recognizes JSON text. This is safer. The JSON official website provides such a script. Address: Http://www.JSON.org/json2.js. Simple to use, after the introduction of the file, such as: Json.parse ($ ("#ctl00_ContentPlaceHolder1_hfSearch"). Val (). toString ()).

4 When using the Json.parse method or the $.parsejson method, note that the name and value of the JSON data are enclosed in double quotes, and for the $.parsejson method, the JSON string is also enclosed in single quotes and then converted, For the Json.parse method, you don't have to. It's a hell of a waste of time.

Note These tips, when manipulating JSON data, you can take a lot less detours.

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.