JSON处理类
来源:互联网 发布:逍遥模拟器连不上网络 编辑:程序博客网 时间:2024/05/16 11:57
import java.io.IOException;import java.io.StringReader;import java.io.StringWriter;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.List;import java.util.Map;import com.fasterxml.jackson.core.JsonFactory;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.ObjectMapper;/** * * <b>类说明:</b>Jackson工具类 * * <p> * <b>详细描述:</b> * * @author liuhuanchao * @since 2016-04-18 */public class JacksonUtil { private static final ObjectMapper MAPPER = new ObjectMapper(); static { MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); } private static final JsonFactory JSONFACTORY = new JsonFactory(); /** * 转换Java Bean 为 json */ public static String beanToJson(Object o) throws JsonParseException { StringWriter sw = new StringWriter(); JsonGenerator jsonGenerator = null; try { jsonGenerator = JSONFACTORY.createJsonGenerator(sw); MAPPER.writeValue(jsonGenerator, o); return sw.toString(); } catch (Exception e) { throw new RuntimeException(e+"转换Java Bean 为 json错误"); } finally { if (jsonGenerator != null) { try { jsonGenerator.close(); } catch (Exception e) { throw new RuntimeException(e+"转换Java Bean 为 json错误"); } } } } /** * json 转 javabean * * @param json * @return */ @SuppressWarnings({ "unchecked", "rawtypes" })public static Object jsonToBean(String json, Class clazz) throws JsonParseException { try {// MAPPER.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); return MAPPER.readValue(json, clazz); } catch (Exception e) { throw new RuntimeException(e+"json 转 javabean错误"); } } /** * 转换Java Bean 为 HashMap */ @SuppressWarnings("unchecked")public static Map<String, Object> beanToMap(Object o) throws JsonParseException { try { return MAPPER.readValue(beanToJson(o), HashMap.class); } catch (Exception e) { throw new RuntimeException(e+"转换Java Bean 为 HashMap错误"); } } /** * 转换Json String 为 HashMap */ @SuppressWarnings("unchecked")public static Map<String, Object> jsonToMap(String json, boolean collToString) throws JsonParseException { Map<String, Object> map = null; try { map = MAPPER.readValue(json, HashMap.class); } catch (IOException e) { throw new RuntimeException(e+"转换Java Bean 为 HashMap错误"); } if (collToString) { for (Map.Entry<String, Object> entry : map.entrySet()) { if (entry.getValue() instanceof Collection || entry.getValue() instanceof Map) { entry.setValue(beanToJson(entry.getValue())); } } } return map; } /** * List 转换成json * * @param list * @return */ public static String listToJson(List<Map<String, String>> list) throws JsonParseException { JsonGenerator jsonGenerator = null; StringWriter sw = new StringWriter(); try { jsonGenerator = JSONFACTORY.createJsonGenerator(sw); new ObjectMapper().writeValue(jsonGenerator, list); jsonGenerator.flush(); return sw.toString(); } catch (Exception e) { throw new RuntimeException(e+"List 转换成json错误"); } finally { if (jsonGenerator != null) { try { jsonGenerator.flush(); jsonGenerator.close(); } catch (Exception e) { throw new RuntimeException(e+"List 转换成json错误"); } } } } /** * json 转List * * @param json * @return */ @SuppressWarnings("unchecked")public static List<Map<String, String>> jsonToList(String json) throws JsonParseException { try { if (json != null && !"".equals(json.trim())) { JsonParser jsonParse = JSONFACTORY.createJsonParser(new StringReader(json)); return (List<Map<String, String>>) new ObjectMapper().readValue(jsonParse, ArrayList.class); } else { throw new RuntimeException("json 转List错误"); } } catch (Exception e) { throw new RuntimeException(e+"json 转List错误"); } }}
0 0
- Json处理工具类
- JSON处理类
- 工具类之json处理
- JSON处理
- 处理json
- json处理
- JSON处理
- JSON处理
- json处理
- 【Util 通用代码】 JSON类 -- JSON对象处理
- Java转成JSON处理类写法模式
- Json解析类(处理时间格式)
- Java JSON处理类库Jackson
- JQuery当做类的处理--JSON数据结构
- java处理json的工具类
- fastjson json高性能处理类
- VBA处理JSON文件的类模块
- Java中JSON处理工具类
- Swifit指纹识别小Demo
- js中使用EL表达式
- 剑指offer之面试题9:斐波那契数列
- boost的配置解析库
- EEPROM-24LC512 family
- JSON处理类
- JDK 1.7 1.8 新特性
- QML中ScrollView的自动滚动
- maven
- 替换UISearchBar的取消按钮
- ubuntu搭建java开发环境
- 如何控制app包的大小
- iOS tabbar和navigation组合,点击item两次,会pop到上一个页面,解决不让控制器跳转的方法
- 关于ListView触摸事件和点击Item事件冲突解决方法