【问题标题】:How to identify and extract boolean, integer etc data from json如何从json中识别和提取布尔、整数等数据
【发布时间】:2016-08-11 10:35:50
【问题描述】:

在 UI 中,我创建了一个对象并将其中一个属性设置为布尔值:

function UserObject(componentId, componentName, checkedOut) {
    this.componentId = componentId;
    this.componentName = componentName;
    this.checkedOut = checkedOut; //this is boolean variable
}

但是从后端,当我在我的对象中设置布尔值时,json 将其转换为字符串。

private UserObject createUserObject(EntityDTO entity) {
    UserObject userObject = new UserObject();
    userObject.setComponentId(entity.getEntityId());
    userObject.setComponentName(entity.getEntityName());
    userObject.setCheckedOut(entity.getCheckedOut());
    return userObject;
}

现在,问题来了,我匹配某些条件两次 (1),然后在从后端获取数据时创建 (2)。每当我匹配“checkedOut”对象的条件时,如果对象来自后端,它就会失败:

if(cell.value.checkedOut === true){
    //some code
}else{
    //some more code
}

我该怎么办?在此先感谢:)

【问题讨论】:

    标签: javascript java angularjs json


    【解决方案1】:
    if(cell.value.checkedOut === "true"){
        //some code
    }else{
        //some more code
    }
    

    由于它是 json 中的字符串,现在使用双引号进行比较

    【讨论】:

    • 但是在创建的情况下它将无法工作,请参阅我匹配条件两次:(1)创建时(2)稍后从后端获取数据。
    【解决方案2】:

    如果要将字符串“真假”转换为布尔类型,请使用eval()

    if(eval(cell.value.checkedOut) === true) {
        //some code
    } else {
        //some more code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-29
      • 2016-08-22
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2019-04-10
      • 2023-03-11
      • 1970-01-01
      相关资源
      最近更新 更多