如何从json中识别和提取布尔,整数等数据

时间:2016-08-11 10:35:51

标签: javascript java angularjs json

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

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;
}

现在,问题是,我在从后端获取数据的同时创建(2)时匹配一些条件两次(1)。每当我匹配“checkedOut”对象的条件时,对象来自后端的情况就会失败:

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

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

2 个答案:

答案 0 :(得分:2)

<li class="add-to-cart">
  <button class="btn btn-add-cart" ng-click="cartCtrl.updateCart(product)" 
    ng-class="cartCtrl.cart[product.id] && 'active'">
    <i class="shopping-cart" aria-hidden="true"></i>
  </button>
</li>

因为它是json中的字符串现在使用双引号进行比较

答案 1 :(得分:0)

如果你想转换字符串&#34;是或否&#34;要布尔类型,请使用eval()

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