总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar、jackson-jaxrs-1.7.1.jar、jackson-mapper-asl-1.7.1.jar这三个jar包
- package com.zuidaima.util.json;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.Reader;
- import java.io.Writer;
- import java.net.URL;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
-
- import org.codehaus.jackson.JsonNode;
- import org.codehaus.jackson.map.DeserializationConfig;
- import org.codehaus.jackson.map.ObjectMapper;
- import org.codehaus.jackson.map.annotate.JsonSerialize;
- import org.codehaus.jackson.map.type.TypeFactory;
-
- public class JsonUtil {
-
- private static ObjectMapper mapper = new ObjectMapper();
- static{
-
-
- mapper.getDeserializationConfig().set(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
-
-
- mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
-
- mapper.getSerializationConfig().setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT);
- }
-
- public static boolean isJSON(String jsonString) {
- return mapper.canSerialize(HashMap.class);
- }
-
- public static <V> Map<String, V> toMap(String content,Class<? extends V> clazz) throws Exception {
- return mapper.readValue(content, TypeFactory.mapType(HashMap.class,String.class, clazz));
- }
-
- public static <V> Map<String, V> toMap(InputStream is,Class<? extends V> clazz) throws Exception {
- return mapper.readValue(is, TypeFactory.mapType(HashMap.class,String.class, clazz));
- }
-
- public static <V> Map<String, V> toMap(Reader is, Class<? extends V> clazz)throws Exception {
- return mapper.readValue(is, TypeFactory.mapType(HashMap.class,String.class, clazz));
- }
-
- public static <V> Map<String, V> toMap(URL is, Class<? extends V> clazz)throws Exception {
- return mapper.readValue(is, TypeFactory.mapType(HashMap.class,String.class, clazz));
- }
-
-
- public static <E> List<E> toList(String content, Class<? extends E> clazz)throws Exception {
- return jsonToList(content, clazz);
- }
-
-
- public static <E> List<E> jsonToList(String content,Class<? extends E> clazz) throws Exception {
- return mapper.readValue(content, TypeFactory.collectionType(ArrayList.class, clazz));
- }
-
- public static Integer[] jsonToIntArray(String content) throws Exception {
- return jsonToArray(content, Integer.class);
- }
-
- public static Integer[] jsonToIntArray(String content,String key) throws Exception {
- return jsonToArray(content, key, Integer.class);
- }
-
- public static <E> E[] jsonToArray(String content, Class<? extends E> clazz)throws Exception {
- if(content != null){
- return mapper.readValue(content, TypeFactory.arrayType(clazz));
- }else{
- return null;
- }
- }
-
-
- public static <T> T fromJsonToObject(String content,Class<? extends T> clazz) throws Exception {
- return jsonToObject(content, clazz);
- }
-
- public static <T> T jsonToObject(String content, Class<? extends T> clazz)throws Exception {
- return mapper.readValue(content, clazz);
- }
-
-
- public static <T> T jsonToObject(String content, String key,Class<? extends T> clazz) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToObject(path.toString(), clazz);
- }else{
- return null;
- }
- }
-
- public static Integer getInt(String content, String key) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToObject(path.toString(), Integer.class);
- }else{
- return null;
- }
- }
-
- public static String getString(String content, String key) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToObject(rootNode.path(key).toString(), String.class);
- }else{
- return null;
- }
- }
-
- public static Date getDate(String content, String key) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToObject(path.toString(), Date.class);
- }else{
- return null;
- }
- }
-
-
- public static <E> E[] jsonToArray(String content, String key,Class<? extends E> clazz) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToArray(rootNode.path(key).toString(), clazz);
- }else{
- return null;
- }
- }
-
- public static Integer[] jsonToArray(String content, String key) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return jsonToArray(path.toString(), Integer.class);
- }else{
- return null;
- }
- }
-
-
- public static <E> List<E> jsonToList(String content, String key,Class<? extends E> clazz) throws Exception {
- JsonNode rootNode = mapper.readValue(content, JsonNode.class);
- JsonNode path = rootNode.path(key);
- if(!path.isMissingNode()){
- return toList(path.toString(), clazz);
- }else{
- return null;
- }
- }
-
-
- public static String toJson(Object o) throws Exception {
- return mapper.writeValueAsString(o);
- }
-
-
- public static void toJson(OutputStream out, Object o) throws Exception {
- mapper.writeValue(out, o);
- }
-
-
- public static void toJson(Writer out, Object o) throws Exception {
- mapper.writeValue(out, o);
- }
-
- public static String map2Json(Map map) throws Exception{
- return toJson(map);
- }
-
- public static String formatJson(String json, String fillStringUnit) {
- if (json == null || json.trim().length() == 0) {
- return null;
- }
-
- int fixedLenth = 0;
- ArrayList<String> tokenList = new ArrayList<String>();
- {
- String jsonTemp = json;
-
- while (jsonTemp.length() > 0) {
- String token = getToken(jsonTemp);
- jsonTemp = jsonTemp.substring(token.length());
- token = token.trim();
- tokenList.add(token);
- }
- }
-
- for (int i = 0; i < tokenList.size(); i++) {
- String token = tokenList.get(i);
- int length = token.getBytes().length;
- if (length > fixedLenth && i < tokenList.size() - 1 && tokenList.get(i + 1).equals(":")) {
- fixedLenth = length;
- }
- }
-
- StringBuilder buf = new StringBuilder();
- int count = 0;
- for (int i = 0; i < tokenList.size(); i++) {
-
- String token = tokenList.get(i);
-
- if (token.equals(",")) {
- buf.append(token);
- doFill(buf, count, fillStringUnit);
- continue;
- }
- if (token.equals(":")) {
- buf.append(" ").append(token).append(" ");
- continue;
- }
- if (token.equals("{")) {
- String nextToken = tokenList.get(i + 1);
- if (nextToken.equals("}")) {
- i++;
- buf.append("{ }");
- } else {
- count++;
- buf.append(token);
- doFill(buf, count, fillStringUnit);
- }
- continue;
- }
- if (token.equals("}")) {
- count--;
- doFill(buf, count, fillStringUnit);
- buf.append(token);
- continue;
- }
- if (token.equals("[")) {
- String nextToken = tokenList.get(i + 1);
- if (nextToken.equals("]")) {
- i++;
- buf.append("[ ]");
- } else {
- count++;
- buf.append(token);
- doFill(buf, count, fillStringUnit);
- }
- continue;
- }
- if (token.equals("]")) {
- count--;
- doFill(buf, count, fillStringUnit);
- buf.append(token);
- continue;
- }
-
- buf.append(token);
-
- if (i < tokenList.size() - 1 && tokenList.get(i + 1).equals(":")) {
- int fillLength = fixedLenth - token.getBytes().length;
- if (fillLength > 0) {
- for(int j = 0; j < fillLength; j++) {
- buf.append(" ");
- }
- }
- }
- }
- return buf.toString();
- }
-
- private static String getToken(String json) {
- StringBuilder buf = new StringBuilder();
- boolean isInYinHao = false;
- while (json.length() > 0) {
- String token = json.substring(0, 1);
- json = json.substring(1);
-
- if (!isInYinHao &&
- (token.equals(":") || token.equals("{") || token.equals("}")
- || token.equals("[") || token.equals("]")
- || token.equals(","))) {
- if (buf.toString().trim().length() == 0) {
- buf.append(token);
- }
-
- break;
- }
-
- if (token.equals("\\")) {
- buf.append(token);
- buf.append(json.substring(0, 1));
- json = json.substring(1);
- continue;
- }
- if (token.equals("\"")) {
- buf.append(token);
- if (isInYinHao) {
- break;
- } else {
- isInYinHao = true;
- continue;
- }
- }
- buf.append(token);
- }
- return buf.toString();
- }
-
- private static void doFill(StringBuilder buf, int count, String fillStringUnit) {
- buf.append("\n");
- for (int i = 0; i < count; i++) {
- buf.append(fillStringUnit);
- }
- }
- }
引用原文:https://blog.csdn.net/springmvc_springdata/article/details/44056043
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
总结的一些json格式和对象/String/Map/List等的互转工具类
原文:https://www.cnblogs.com/summary-2017/p/8965074.html