标签:现在 close except swa set value 除了 actor utils
Jackson fasterxml和codehaus的区别: 他们是Jackson的两大分支、也是两个版本的不同包名。Jackson从2.0开始改用新的包名fasterxml;
1.x版本的包名是codehaus。除了包名不同,他们的Maven artifact id也不同。1.x版本现在只提供bug-fix,
2.x版本还在不断开发和发布中。如果是新项目,建议直接用2x,即fasterxml jackson。
P.S. Jackson可以轻松的将Java对象转换成json对象和xml文档,同样也可以将json、xml转换成Java对象。
官网:http://wiki.fasterxml.com/JacksonHome 三个主要的模块: jackson-co re:核心包 jackson-annotations:注解包 jackson-databind:数据绑定包 jackson-databind需要引用另外两个包,所以如果项目中需要jackson-databind,则只需要加入它的dependency就行了,其他两个会自动引入: <dependency> <groupId>com.fasterxml.jackson.
使用(java jar-jackson-all-1.9.0jar自行下载)
import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.map.MappingJsonFactory; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.type.JavaType; public class JsonUtils { public static <K, V> String toJson(Map<K, V> obj) { String jsonString = ""; try { if (obj == null) { return ""; } StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createJsonGenerator(sw); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(gen, obj); jsonString = sw.toString(); sw.close(); } catch (Exception ex) { ex.printStackTrace(); return ""; } return jsonString; } @SuppressWarnings("unchecked") public static Map<String, Object> toMap(String json) { Map<String, Object> result = new HashMap<String, Object>(); try { if (json == null || json.equals("")) { return null; } ObjectMapper objectMapper = new ObjectMapper(); result = objectMapper.readValue(json, Map.class); if (result == null) { return new HashMap<String, Object>(); } } catch (Exception ex) { ex.printStackTrace(); } return result; } public static String toJson(Object object) { String json = ""; try { if (object == null) { return ""; } StringWriter sw = new StringWriter(); JsonGenerator gen = new JsonFactory().createJsonGenerator(sw); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(gen, object); json = sw.toString(); sw.close(); } catch (Exception ex) { ex.printStackTrace(); json = ""; } return json; } public static <T> T toObject(String json, Class<T> clazz) { T obj = null; try { if (json == null || json.equals("")) { return null; } JsonFactory jsonFactory = new MappingJsonFactory(); JsonParser jsonParser = jsonFactory.createJsonParser(json); ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES,false); //mapper.getDeserializationConfig().set(Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); obj = mapper.readValue(jsonParser, clazz); } catch (Exception ex) { ex.printStackTrace(); obj = null; } return obj; } public static <T> List<T> toList(String json, Class<T> clazz) { if (json == null || json.equals("")) { return null; } ObjectMapper mapper = new ObjectMapper(); mapper.configure(Feature.FAIL_ON_UNKNOWN_PROPERTIES,false); List<T> list = new ArrayList<T>(); try { JavaType type = mapper.getTypeFactory().constructParametricType(List.class, clazz); list = mapper.readValue(json, type); return list; } catch (Exception e) { e.printStackTrace(); } return list; } }
标签:现在 close except swa set value 除了 actor utils
原文地址:https://www.cnblogs.com/fanblogs/p/11153010.html