JSON passes the processing method of bool type data Introduction _javascript Tips

Source: Internet
Author: User
Other data types are not a problem when using JSON to pass data in Ajax, but if the server-side generated JSON has data of type bool, there is a small problem when parsing the client, summarized as follows:

The JSON returned by the server is:
Copy Code code as follows:

{"typeID": [1037], "Title": "Hebei software vocational and Technical College", "Intro": "", "Islink": "false", "Linkurl": "http://www.hbsi.edu.cn", "Ispic": " True "," Picture ":"/newsimages/hbsi.jpg "," Content ":" <p><br></p> "}

Where properties: Islink and Ispic are all bool types, use the method on the client:
Copy Code code as follows:

document.getElementById ("CheckBox1"). Checked = news. Islink;

The check box is selected, but Islink is false and should not be selected.

For the reason, JavaScript has three basic types of data (string string, numeric number, Boolean Boolean), two reference data types (object, array array), and two special data types (Null, Undefined). Other types of conversions to the bool type have the following principles:

Values converted to bool by data type
Null FALSE
Undefined FALSE
Object TRUE
function TRUE
0 FALSE
1 TRUE
Numbers from 0, 1 TRUE
String TRUE
"" (empty string) FALSE

At this point, Islink is the string "false" in JSON, so the bool type is true after the conversion.

Treatment methods:
Copy Code code as follows:

document.getElementById ("CheckBox1"). Checked = news. islink== "true";

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.