Home > Web Front-end > JS Tutorial > body text

Introduction to the processing method of JSON transferring bool type data_javascript skills

WBOY
Release: 2016-05-16 17:22:10
Original
1930 people have browsed it

When using json to transmit data in ajax, other data types are not a problem. However, if there is bool type data in the JSON generated by the server, there will be a small problem when the client parses it. The summary is as follows:

The JSON returned by the server is:

Copy code 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":"


"}

where Attributes: IsLink and IsPic are both bool types. How to use them on the client:
Copy code The code is as follows:

document.getElementById("checkbox1").checked = news.IsLink;

The checkbox will be selected, but IsLInk is false and it should not be selected. Why?

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

The value after the data type is converted to bool
null FALSE
undefined FALSE
Object TRUE
function TRUE
0 FALSE
1 TRUE
Number other than 0 and 1 TRUE
String TRUE
""(empty string) FALSE

At this time, IsLink is a string in JSON "false", so after conversion, the bool type true is obtained.

Solution:
Copy code The code is as follows:

document.getElementById ("checkbox1").checked = news.IsLink=="true";
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template