简体   繁体   English

如何针对JSON使用JavaScript中的if条件?

[英]How to use if condition in javascript with respect to json?

I have the following json file. 我有以下json文件。

{
    "home_page_boolean":true,
    "syllabus_page_boolean":true,
    "schedule_page_boolean":true,
    "hws_page_boolean":true,
    "project_page_boolean":true,
    "banner_school_image_file":"",
    "left_footer_image_file":"",
    "right_footer_image_file":""
}

I'm trying to check wether one of the booleans is true or false. 我正在尝试检查布尔值之一是对还是错。 Notice that the way I have it now is that the value for the booleans is a boolean value and not a string value, (its not "project_page_boolean":"true") 请注意,我现在拥有的方式是布尔值是布尔值而不是字符串值(它不是“ project_page_boolean”:“ true”)

This is what I tried, 这就是我尝试过的

if(data.homePageBoolean === true){
    navbar+= "<a id='home_link' class='open_nav' href='index.html'>Home</a>";
}

and

if(data.homePageBoolean.toString() === "true"){
    navbar+= "<a id='home_link' class='open_nav' href='index.html'>Home</a>";
}

but It doesn't work. 但这不起作用。 I'm pretty sure the solution is simple but I can't figure it out 我很确定解决方案很简单,但我不知道

Try this 尝试这个

if(data['home_page_boolean']){
    navbar+= "<a id='home_link' class='open_nav' href='index.html'>Home</a>";
}

I think it should be as follows : 我认为应该如下:

if(data.home_page_boolean === true){
    navbar+= "<a id='home_link' class='open_nav' href='index.html'>Home</a>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM