2014-03-27 259 views
2

我在Jersey应用程序中使用Jackson进行JSON序列化/反序列化。我想在我的Java POJO属性中将JSON中的空字符串读取为空值。我试图在Object Mapper上设置DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT,但它不起作用。这里是低于Jackson对象映射器反序列化配置.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

import java.io.IOException; 
import org.codehaus.jackson.JsonParseException; 
import org.codehaus.jackson.map.DeserializationConfig; 
import org.codehaus.jackson.map.JsonMappingException; 
import org.codehaus.jackson.map.ObjectMapper; 

public class TestMapper { 

    /** 
    * @param args 
    */ 
    public static void main(String[] args) { 
     TestMapper.testJson(); 

    } 

    public static void testJson(){ 
     String jsonString = "{\"name\":\"First Name\",\"phone\":\"\",\"unknown\":\"test\"}"; 
     ObjectMapper result = new ObjectMapper(); 
     //result.setDeserializationConfig(result.getDeserializationConfig().with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)); 
     //result.enable(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); 
     /*DeserializationConfig deserializeConfig = result.getDeserializationConfig(); 
     deserializeConfig.with(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);*/ 
     result.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); 
     result.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 

    try { 
      TestBean testBean = result.readValue(jsonString, TestBean.class); 
      if(testBean.getPhone()!=null&& testBean.getPhone().equals("")){ 
       System.out.print("Phone Number is empty string"); 
      }else{ 
       System.out.print("Phone Number is null"); 
      } 
     } catch (JsonParseException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (JsonMappingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

} 

代码的bean是如下

public class TestBean { 

    private String name; 
    private String phone; 
    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getPhone() { 
     return phone; 
    } 
    public void setPhone(String phone) { 
     this.phone = phone; 
    } 
    public TestBean(String name, String phone) { 
     super(); 
     this.name = name; 
     this.phone = phone; 
    } 
    public TestBean() { 
     super(); 
    } 


} 

输出始终

Phone Number is empty string 

我不知道这是为什么不工作。我正在使用杰克逊1.9.2。

+0

你有没有调试过,看看手机有什么价值? – Jay

+0

是的值是“” – user2957698

+0

我有同样的问题,任何解决方案?我正在使用杰克逊1.9 –

回答

3

注意,可经启用以允许JSON空字符串值ACCEPT_EMPTY_STRING_AS_NULL_OBJECT

特征的Javadoc(“”)被绑定到的POJO为空。

因此,空的String可用于将null映射到POJO。 A String不是POJO,它被认为是JSON原始值。

你不能在这里使用。

ACCEPT_EMPTY_STRING_AS_NULL_OBJECT将工作像

{"name":"First Name","phone":"","unknown":"test"} 

在哪里,你会做出一些POJO类的TestBean.phone场像

class Phone { 
    private String number; 
} 

然后反序列化会使得phone场是null

+3

有没有办法实现我想要做的事情? – user2957698

+0

@ user2957698并不如我所知。而你可能不应该。 (可能修复您的服务器以返回不同的JSON)。或者,在反序列化之后,你可以使用一个辅助函数来检查'String'是否为空或'null'。 –

+0

我遇到同样的问题,对此有何解决方案?我正在使用杰克逊1.9 –