Home  >  Article  >  Web Front-end  >  Introduction to the processing method of JSON transferring bool type data_javascript skills

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

WBOY
WBOYOriginal
2016-05-16 17:22:101899browse

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";
Statement:
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