浏览 5216 次
锁定老帖子 主题:JSON工具类
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (1)
|
|
---|---|
作者 | 正文 |
发表时间:2012-04-03
/** public static Object jsonToObject(String jsonString, Class<?> classes) { 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-04-05
/** * fromJsonToObject<br> * jackjson把json字符串转换为Java对象的实现方法 * * <pre> * return Jackson.jsonToObj(this.answersJson, new TypeReference<List<StanzaAnswer>>() { * }); * </pre> * * @param <T> * 转换为的java对象 * @param json * json字符串 * @param typeReference * jackjson自定义的类型 * @return 返回Java对象 */ public static <T> T jsonToObj(String json, TypeReference<T> typeReference) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(json, typeReference); } catch (JsonParseException e) { logger.error("JsonParseException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromJsonToObject<br> * json转换为java对象 * * <pre> * return Jackson.jsonToObj(this.answersJson, Jackson.class); * </pre> * * @param <T> * 要转换的对象 * @param json * 字符串 * @param valueType * 对象的class * @return 返回对象 */ public static <T> T jsonToObj(String json, Class<T> valueType) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(json, valueType); } catch (JsonParseException e) { logger.error("JsonParseException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromObjectToJson<br> * java对象转换为json字符串 * * @param object * Java对象 * @return 返回字符串 */ public static String objToJson(Object object) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(object); } catch (JsonGenerationException e) { logger.error("JsonGenerationException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromObjectToJson<br> * java对象转换为json字符串 * * @param object * 要转换的对象 * @param filterName * 过滤器的名称 * @param properties * 要过滤哪些字段 * @return */ @SuppressWarnings("deprecation") public static String objToJson(Object object, String filterName, Set<String> properties) { ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider().addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(properties)); try { return mapper.filteredWriter(filters).writeValueAsString(object); } catch (JsonGenerationException e) { logger.error("JsonGenerationException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } |
|
返回顶楼 | |
发表时间:2012-04-05
jackson 用起来还是挺不错的
|
|
返回顶楼 | |
发表时间:2012-04-13
matychen 写道
/** * fromJsonToObject<br> * jackjson把json字符串转换为Java对象的实现方法 * * <pre> * return Jackson.jsonToObj(this.answersJson, new TypeReference<List<StanzaAnswer>>() { * }); * </pre> * * @param <T> * 转换为的java对象 * @param json * json字符串 * @param typeReference * jackjson自定义的类型 * @return 返回Java对象 */ public static <T> T jsonToObj(String json, TypeReference<T> typeReference) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(json, typeReference); } catch (JsonParseException e) { logger.error("JsonParseException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromJsonToObject<br> * json转换为java对象 * * <pre> * return Jackson.jsonToObj(this.answersJson, Jackson.class); * </pre> * * @param <T> * 要转换的对象 * @param json * 字符串 * @param valueType * 对象的class * @return 返回对象 */ public static <T> T jsonToObj(String json, Class<T> valueType) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.readValue(json, valueType); } catch (JsonParseException e) { logger.error("JsonParseException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromObjectToJson<br> * java对象转换为json字符串 * * @param object * Java对象 * @return 返回字符串 */ public static String objToJson(Object object) { ObjectMapper mapper = new ObjectMapper(); try { return mapper.writeValueAsString(object); } catch (JsonGenerationException e) { logger.error("JsonGenerationException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } /** * fromObjectToJson<br> * java对象转换为json字符串 * * @param object * 要转换的对象 * @param filterName * 过滤器的名称 * @param properties * 要过滤哪些字段 * @return */ @SuppressWarnings("deprecation") public static String objToJson(Object object, String filterName, Set<String> properties) { ObjectMapper mapper = new ObjectMapper(); FilterProvider filters = new SimpleFilterProvider().addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(properties)); try { return mapper.filteredWriter(filters).writeValueAsString(object); } catch (JsonGenerationException e) { logger.error("JsonGenerationException: ", e); } catch (JsonMappingException e) { logger.error("JsonMappingException: ", e); } catch (IOException e) { logger.error("IOException: ", e); } return null; } FilterProvider filters = new SimpleFilterProvider().addFilter(filterName, SimpleBeanPropertyFilter.serializeAllExcept(properties)); 写完这个后,还需要在需过滤的Bean上加@JsonFilter(filterName), 虚心请教下能不能更简单点。 |
|
返回顶楼 | |