如何在JSON中使用if语句?

如何在JSON中使用if语句这是代码:...............................................

..................................................................

var config =

[

{

"name" : "SiteTitle",

"bgcolor" : "",

"color" : "",

"position" : "TL",

"text" : "step1",

"time" : 5000

},

{

"name" : "Jawal",

"bgcolor" : "",

"color" : "",

"text" : "step2",

"position" : "BL",

"time" : 5000

},

{

"name" : "Password",

"bgcolor" : "",

"color" : "",

"text" : "step3",

"position" : "TL",

"time" : 5000

}

],

//define if steps should change automatically

autoplay = false,

//timeout for the step

showtime,

//current step of the tour

step = 0,

//total number of steps

total_steps = config.length;

这是必需的结果,如下所示:

    var config =

[

if(page==true) {

{

"name" : "SiteTitle",

"bgcolor" : "",

"color" : "",

"position" : "TL",

"text" : "step1",

"time" : 5000

},

{

"name" : "Jawal",

"bgcolor" : "",

"color" : "",

"text" : "step2",

"position" : "BL",

"time" : 5000

}

} else {

{

"name" : "Password",

"bgcolor" : "",

"color" : "",

"text" : "step3",

"position" : "TL",

"time" : 5000

}

}

],

//define if steps should change automatically

autoplay = false,

//timeout for the step

showtime,

//current step of the tour

step = 0,

//total number of steps

total_steps = config.length;

实际上,这种方式是错误的,并且会导致JavaScript语法错误。

回答:

那是普通的JavaScript,而不是JSON。将if语句移到外部:

if (page) {

var config = [

{

"name" : "SiteTitle",

"bgcolor" : "",

"color" : "",

"position" : "TL",

"text" : "step1",

"time" : 5000

}, {

"name" : "Jawal",

"bgcolor" : "",

"color" : "",

"text" : "step2",

"position" : "BL",

"time" : 5000

}

];

} else {

var config = [

{

"name" : "Password",

"bgcolor" : "",

"color" : "",

"text" : "step3",

"position" : "TL",

"time" : 5000

}

];

}

以上是 如何在JSON中使用if语句? 的全部内容, 来源链接: utcz.com/qa/433829.html

回到顶部