如何在JSON中使用if语句?

Mon*_*awy 8 javascript jquery 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;
Run Code Online (Sandbox Code Playgroud)

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

    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;
Run Code Online (Sandbox Code Playgroud)

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

Eri*_*eni 9

验证JSON Schema Draft-07,JSON现在支持if...then...else条件数据表示的关键字.

{
    "type": "integer",
    "minimum": 1,
    "maximum": 1000,
    "if": { "minimum": 100 },
    "then": { "multipleOf": 100 },
    "else": {
        "if": { "minimum": 10 },
        "then": { "multipleOf": 10 }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ble*_*der 6

这是常规的 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
        }
    ];
}
Run Code Online (Sandbox Code Playgroud)

  • @MontaserEl-sawy:不,你明白了。 (6认同)