How to process bool data transmitted in JSON

Source: Internet
Author: User

When json is used to transmit data in ajax, other data types are not a problem. However, if the JSON generated by the server contains bool data, a small problem occurs during client parsing, summary:

The JSON returned by the server is:
Copy codeThe Code is 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> "}

Here, attributes: IsLink and IsPic are both of the bool type. How to use them on the client:
Copy codeThe Code is as follows:
Document. getElementById ("checkbox1"). checked = news. IsLink;

Check box is selected, but IsLInk is false. This check box should not be selected. Why?

Check the cause. javascript has three basic data types (string, number, and boolean) and two reference data types (Object and Array) and two special data types (Null and Undefined ). The following principles apply when converting other types to the bool type:

Value after the data type is converted to bool
Null FALSE
Undefined FALSE
Object TRUE
Function TRUE
0 FALSE
1 TRUE
A number other than 0 and 1 is TRUE.
String TRUE
"" (Null String) FALSE

In this case, IsLink is the string "false" in JSON, so the bool type is true after conversion.

Solution:
Copy codeThe Code is 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.