简体   繁体   English

使用Jackson JSON解析为布尔值为TRUE或FALSE

[英]TRUE or FALSE into boolean using Jackson JSON parsing

I am using Jackson annotation for parsing JSON response into POJO object.I was using boolean variable in POJO for mapping values "true" and "false" coming from JSON. 我正在使用Jackson注释将JSON响应解析为POJO对象。我在POJO中使用布尔变量来映射来自JSON的值“true”和“false”。 But suddenly we are getting value as "TRUE" and "FALSE" into JSON and parsing failing for these values. 但突然之间,我们将JST中的值变为“TRUE”和“FALSE”,并解析了这些值的失败。 Can anyone suggest way to map it to boolean as this variable is used so many places where i don't want to change logic to String to Boolean . 任何人都可以建议将它映射到布尔值的方法,因为这个变量被用在很多地方,我不想将逻辑更改为String到Boolean。

It isn't really an issue, this is basically the way BeanUtils works. 这不是一个真正的问题,这基本上是BeanUtils的工作方式。

For boolean vars, Jackson removes is from the setter name to derive what it expects the variable name to be when marshalling to JSON and adds set to that same derived name to unmarshal back to a POJO. 对于boolean瓦尔,杰克逊删除is从模子名获得哪些,预计变量名编组,以JSON时候要,并增加了set于同一来源的名称来解组回POJO。

So boolean isFooTrue; 所以boolean isFooTrue; ends up as fooTrue when marshalled to JSON, and when unmarshalling it would attempt to call setIsFooTrue(); 当编组到JSON时结束为fooTrue ,并且当解组时它将尝试调用setIsFooTrue(); , which isn't the correct. ,这是不正确的。

If you're using an IDE and you generated your getter/setters, you'll probably notice that the generated code for boolean isFoo; 如果您正在使用IDE并且生成了getter / setter,那么您可能会注意到生成的boolean isFoo;代码boolean isFoo; basically ignores the is as if the var name was just foo : 基本上忽略了is好像var名称只是foo

private boolean isFoo;

public boolean isFoo() {
    return isFoo;
}

public void setFoo(boolean isFoo) {
    this.isFoo= isFoo;
}

Two options are to remove the is from the var name, or add the is to the setter name. 有两个选项is从var名称中删除is ,或者将is添加到setter名称。

I am not sure this is what you want. 我不确定这是你想要的。 But it works. 但它的确有效。

Boolean param = Boolean.parseBoolean((String)yourValue);

The tested code is 经过测试的代码是

public class program10 {

    public static void main(String args[]) {

        String yourValue = "TRUE"; // This is what you get from json.
        Boolean param = Boolean.parseBoolean((String)yourValue);

        if(param == true) 
            System.out.println("Value is true");
        else
            System.out.println("Value is false");
        System.out.println(param);
    }
}

I also faced a similiar issue using Jackson Parser 1.8.5. 我还遇到了使用Jackson Parser 1.8.5的类似问题。 Java POJO to JSON worked but same JSON back to Java POJO did not. Java POJO到JSON工作但JSON回到Java POJO没有。 In Java POJO, if a boolean variable is declared as 在Java POJO中,如果布尔变量声明为

private Boolean isMyVar;

then the Jackson produces equivalent JSON as 然后杰克逊产生等效的JSON

{..,
"myVar" : false,
..
}

(I know the boolean variable naming is wrong here, but the JAR is third party and say you cannot change it!) (我知道布尔变量命名在这里是错误的,但是JAR是第三方,并说你不能改变它!)

I think this is an issue with the way Jackson parser is designed to handle boolean values. 我认为这是Jackson解析器设计用于处理布尔值的方式的问题。 I changed the JSON from "myVar" : false to "isMyVar" : false and it worked ok to create back the Java POJO from the JSON. 我将JSON从“myVar”更改为:false为“isMyVar”:false并且可以从JSON创建Java POJO。

Anybody knows if this is still a bug or has it been resolved? 有谁知道这仍然是一个错误还是已经解决了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM