一尘不染

如何在JSON中使用if语句?

json

如何在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语法错误。


阅读 1525

收藏
2020-07-27

共1个答案

一尘不染

那是普通的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
        }
    ];
}
2020-07-27