jackson json字符串 转换Bean, Bean 里没有对应的值

	private static ObjectMapper mapper;
	public static ObjectMapper getObjectMapper(){
		if(null == mapper){
			mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);
			mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		}
		
		return mapper;
	}
	
    /**   
     * 获取泛型的Collection Type  
     * @param collectionClass 泛型的Collection   
     * @param elementClasses 元素类   
     * @return JavaType Java类型   
     * @since 1.0   
     */   
    

	 public static Object convertToCollection(String jsonString ,Class<?> collectionClass, Class<?>... elementClasses) throws JsonParseException, JsonMappingException, IOException{
		 Object result = new Object();
		 JavaType javaType = getObjectMapper().getTypeFactory().constructParametricType(collectionClass, elementClasses);
		 result = getObjectMapper().readValue(jsonString, javaType);
		 
		 return result;
	 }


mapper = new ObjectMapper().setVisibility(JsonMethod.FIELD, Visibility.ANY);

mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);



<dependency>  
    <groupId>org.codehaus.jackson</groupId>  
    <artifactId>jackson-core-asl</artifactId>  
    <version>1.9.13</version>  
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

猜你喜欢

转载自blog.csdn.net/wuyezhiyu/article/details/80528020