Json对象条件



我想使用AND条件创建一个Json对象(我不知道是否可能)。我把我现在没有完成的代码。

var parameters = {
                Title : "hello",
                Text : "world",
                Question : //if two checkboxes are checked then true else false.
}

关于选中的两个复选框,我想用这个:

$('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')

我想避免的是使用像

这样的if
//if checkboxes are checked
//{
//create the json object of type 1
//}
//else
//{
//create the json object of type 2
//}

这可能吗?

是的,布尔值可以:

var parameters = {
            Title : "hello",
            Text : "world",
            Question : $('.checkbox1').is(':checked') && $('.checkbox2').is(':checked')
}

这将创建一个普通的JavaScript对象,你可以像操作其他对象一样操作它,例如

parameters.Question = <newTrueOrFalseToSet>;

甚至:

parameters["Question"] = <newTrueOrFalseToSet>;

根据JSON定义,生成的JSON应该是这样的:
{
    "Title": "hello",
    "Text": "world",
    "Question": true
}