JSON data types and how to pass bool type data processing

Source: Internet
Author: User

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

The JSON returned by the server is:

The 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> "}


Where properties: Islink and Ispic are all bool types, the client uses the method:

The code is as follows:
document.getElementById ("CheckBox1"). Checked = news. Islink;


check box is selected, but Islink is false, this should not be selected, why?

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

Value after the data type is converted to bool
Null FALSE
Undefined FALSE
Object TRUE
function TRUE
0 FALSE
1 TRUE
0, 1 digits outside TRUE
String TRUE
"" (empty string) FALSE

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

Treatment methods:

The code is as follows:
document.getElementById ("CheckBox1"). Checked = news. islink== "true";

JSON data types and how to pass bool type data processing

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.