Processing Method for passing bool type data 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:

{"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:

Document. getelementbyid ("checkbox1"). Checked = News. islink; check box 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:

 

Data Type Value After bool Conversion
Null False
Undefined False
Object True
Function True
0 False
1 True
Numbers other than 0 and 1 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:

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.