14

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

4

6 回答 6

16

验证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 }
    }
}

编辑

在上下文中使用 OP 的示例,它可以这样写:

{
    "if": {
        "page": true
    },
    "then": [
        {
            "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
        }
    ]
}
于 2018-09-07T08:01:41.573 回答
4

那是常规的 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
        }
    ];
}
于 2013-02-25T07:28:20.630 回答
0

或者这个:

 var config =
  (page == true) ?
     [             
        {
            "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
        }
    ];
于 2013-02-25T07:32:24.630 回答
0

您也可以这样做(不是说这是最好的方法,而是另一种方法,在某些情况下可能有用)

let config = [];
if (page) {
   config.push({
        "name"      : "SiteTitle",
        "bgcolor"   : "",
        "color"     : "",
        "position"  : "TL",
        "text"      : "step1",
        "time"      : 5000
    });
   config.push({
        "name"      : "Jawal",
        "bgcolor"   : "",
        "color"     : "",
        "text"      : "step2",
        "position"  : "BL",
        "time"      : 5000
    });
} else {
    config.push({
        "name"      : "Password",
        "bgcolor"   : "",
        "color"     : "",
        "text"      : "step3",
        "position"  : "TL",
        "time"      : 5000
    });
}
于 2017-11-14T14:31:51.493 回答
0

您可以在 JSON 中添加 if 。

const config = [
...(page ? [
     {
        "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
    }]),
];
于 2021-05-31T12:36:55.340 回答
0

您可以使用一个库jsoncode,它允许您将逻辑表达式直接应用到 JSON 中,并根据传输的模型获得必要的结果:

import jsoncode from './jsoncode.lib.mjs';

const json_src = {
    "configs [AS ARRAY]": {
        "[...IF page]": [
            {
                "name": "SiteTitle",
                "bgcolor": "",
                "color": "",
                "position": "TL",
                "text": "step1",
                "time": 5000
            }, {
                "name": "Jawal",
                "bgcolor": "",
                "color": "",
                "text": "step2",
                "position": "BL",
                "time": 5000
            }
        ],
        "[IF !page]": {
            "name": "Password",
            "bgcolor": "",
            "color": "",
            "text": "step3",
            "position": "TL",
            "time": 5000
        }
    }
};

const configsA = jsoncode(json_src, { page: true  }).configs;
const configsB = jsoncode(json_src, { page: false }).configs;

console.log('configsA:', configsA);
console.log('configsB:', configsB);

https://www.npmjs.com/package/jsoncode

于 2022-02-16T11:49:06.633 回答