标签:序列 ack div type system ash ast main try
1.序列化复杂的Map时,可能需要下面的方式
import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class JsonHelper { public static final ObjectMapper JsonMapper = new ObjectMapper(); /** * String类型的javaType */ public static final JavaType StringJavaType = JsonMapper.getTypeFactory().constructType( String.class); /** * 支持Map<String,String> */ public static final JavaType StringStringMap = JsonMapper.getTypeFactory().constructMapType(HashMap.class, String.class, String.class); /** * 支持Map<String,Map<String,String>> */ public static final JavaType StringStringStringMap = JsonMapper.getTypeFactory() .constructMapType(HashMap.class, StringJavaType, StringStringMap); /** * 支持Map<String,Map<String,Map<String,String>>> */ public static final JavaType StringStringStringStringMap = JsonMapper.getTypeFactory() .constructMapType(HashMap.class, StringJavaType, StringStringStringMap); /** * 支持List<Object> */ public static final JavaType ListObject = JsonMapper.getTypeFactory().constructCollectionType(ArrayList.class, Object.class); /** * 支持List<List<Object>> */ public static final JavaType ListListObject = JsonMapper.getTypeFactory().constructCollectionType(ArrayList.class, ListObject); public static void main(String[] args) { String str = "{\n" + "\"1\":\n" + "{\n" + "\"key1\": \n" + "\t{\n" + "\t \n" + " \"a\": \"1\",\n" + " \"b\": \"1\",\n" + " \"c\": \"a\",\n" + " \"d\": \"2\",\n" + " \"e\": \"1\"\n" + " },\n" + " \"key2\": {\n" + " \"a\": \"1\",\n" + " \"b\": \"1\",\n" + " \"c\": \"b\",\n" + " \"d\": \"2\",\n" + " \"e\": \"1\"\n" + " }\n" + " \n" + "}\n" + "}"; try { Map<String, Map<String, Map<String, String>>> map = (Map<String, Map<String, Map<String, String>>>) JsonMapper .<Map<String, Map<String, Map<String, String>>>>readValue(str, StringStringStringStringMap); Map<String, String> stringStringMap = map.get("1").get("key1"); System.out.println(stringStringMap); } catch (IOException e) { System.out.println("序列化出错"); } }
标签:序列 ack div type system ash ast main try
原文地址:https://www.cnblogs.com/zhshlimi/p/10300159.html