json的工具: jackson的使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
Java代码 package com.rh.util; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * Created with IntelliJ IDEA. User: pandy Date: 13-7-6 Time: 下午5:26 To change * this template use File | Settings | File Templates. */ public class JSONUtils { private static ObjectMapper getObjectMapper() { ObjectMapper mapper = new ObjectMapper(); return mapper; } public static String bean2str(Object obj) { try { ObjectMapper mapper = getObjectMapper(); StringWriter writer = new StringWriter(); JsonGenerator gen = new JsonFactory().createJsonGenerator(writer); mapper.writeValue(gen, obj); gen.close(); String json = writer.toString(); writer.close(); return json; } catch (Exception e) { e.printStackTrace(); return null; } } public static Object str2bean(String json, Class<?> clazz) { try { ObjectMapper mapper = getObjectMapper(); Object domain = mapper.readValue(json, clazz); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } public static Object str2list(String json, TypeReference valueTypeRef) { try { ObjectMapper mapper = getObjectMapper(); Object domain = mapper.readValue(json, valueTypeRef); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } public static Object str2list(String json, Class<?> clazz) { try { ObjectMapper mapper = getObjectMapper(); JavaType type = mapper.getTypeFactory().constructCollectionType(ArrayList.class, clazz); Object domain = mapper.readValue(json, type); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } // new TypeReference<Map<String,Object>>() { } public static Object str2map(String json, TypeReference valueTypeRef) { try { ObjectMapper mapper = getObjectMapper(); Object domain = mapper.readValue(json, valueTypeRef); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } public static Map<String, Object> str2map(String json) { try { ObjectMapper mapper = getObjectMapper(); Map<String, Object> domain = mapper.readValue(json, new TypeReference<Map<String, Object>>() { }); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } public static String list2JsonString(List l) { StringWriter sw = new StringWriter(); try { ObjectMapper mapper = getObjectMapper(); mapper.writeValue(sw, l); } catch (Exception e) { e.printStackTrace(); } return sw.toString(); } public static Object map2bean(Map map, Class<?> clazz) { String json = map2str(map); try { ObjectMapper mapper = getObjectMapper(); Object domain = mapper.readValue(json, clazz); return domain; } catch (Exception e) { e.printStackTrace(); return null; } } public static String map2str(Map map) { StringWriter sw = new StringWriter(); try { ObjectMapper mapper = getObjectMapper(); mapper.writeValue(sw, map); } catch (Exception e) { e.printStackTrace(); } return sw.toString(); } public static Map bean2map(Object o){ String s = bean2str(o); Map map = str2map(s); return map; } } |