在將 json 字符串轉爲對象時,若是對象含有泛型,在進行轉換時須要指明泛型類型。json
/** * @Describe: * @Author: chenfan * @Date: 2019/5/9 19:15 */ @Data /** * 含有泛型屬性的對象 */ class OneGeneric<E>{ E e; String ss; } @Data /** * 泛型對象 */ class KeyMessage{ String kk; } public class JsonTest { public static void main(String[] args) { // {"e": {"kk":"qwe"},"ss": "ssValue"} String data = "{\"e\": {\"kk\":\"qwe\"},\"ss\": \"ssValue\"}"; Gson gson = new Gson(); // OneGeneric : 外層對象類型 KeyMessage :嵌套的泛型類型 OneGeneric one = gson.fromJson(data, new TypeToken<OneGeneric<KeyMessage>>() {}.getType()); System.out.println(one); } }
正確轉換以下:ide
此時和只有一個泛型對象時處理方式同樣測試
public class JsonTest { public static void main(String[] args) { // {"ss": "ssValue","list":[{"dd":"dd1"},{"dd":"dd2"}]} String data = "{\"ss\": \"ssValue\",\"list\":[{\"dd\":\"dd1\"},{\"dd\":\"dd2\"}]}"; Gson gson = new Gson(); OneGeneric grandMessage = gson.fromJson(data, new TypeToken<OneGeneric<Generic>>() {}.getType()); System.out.println(grandMessage); } } /** * 含有泛型屬性的對象 */ @Data class OneGeneric<E>{ List<E> list; String ss; } @Data class Generic{ String dd; }
/** * @Describe: * @Author: chenfan * @Date: 2019/5/9 19:15 */ @Data /** * 含有多個泛型屬性的對象 */ class GrandMessage<K,V>{ K key; V value; } /** * 泛型對象 */ @Data class KeyMessage{ String kk; } /** * 泛型對象 */ @Data class ValueMessage{ String vv; } /** * 測試類 */ public class JsonTest { public static void main(String[] args) { // {"key": {"kk":"qwe"},"value": {"vv":"asd"}} String data = "{\"key\": {\"kk\":\"qwe\"},\"value\": {\"vv\":\"asd\"}}"; Gson gson = new Gson(); GrandMessage grandMessage = gson.fromJson(data, new TypeToken<GrandMessage<KeyMessage,ValueMessage>>() {}.getType()); System.out.println(grandMessage); } }
在進行對象轉換時, TypeToken<GrandMessage<KeyMessage,ValueMessage>> 中的泛型類型順序必須按照 GrandMessage<K,V> 中聲明的順序,不然會對象屬性所有爲nullui
@Data class GrandMessage<K>{ K k; String msg; } @Data class OneGeneric<E>{ E e; String one; } @Data class Generic{ String dd; } /** * 測試類 */ public class JsonTest { public static void main(String[] args) { // {"msg":"lili","k":{"one":"111","e":{"dd":"dddd"}}} String data = "{\"msg\":\"lili\",\"k\":{\"one\":\"111\",\"e\":{\"dd\":\"dddd\"}}}"; Gson gson = new Gson(); // 轉換時的泛型順序必定要嚴格按照對象的嵌套順序 GrandMessage grandMessage = gson.fromJson(data, new TypeToken<GrandMessage<OneGeneric<Generic>>>() {}.getType()); System.out.println(grandMessage); } }
JSON轉對象時,若是對象某個屬性不肯定(好比對象定義爲Object,實際爲 int 類型),須要自定義轉換邏輯spa
顯示指定 OpSessionControlMessage 的 messageId 爲 int 類型:3d
private static Gson gson2 = new GsonBuilder() .registerTypeAdapter( new TypeToken<OpSessionControlMessage>(){}.getType(), new JsonDeserializer<OpSessionControlMessage>() { @Override public OpSessionControlMessage deserialize( JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { OpSessionControlMessage message = new OpSessionControlMessage(); JsonObject jsonObject = json.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet(); for (Map.Entry<String, JsonElement> entry : entrySet) { Object ot = entry.getValue(); if(entry.getKey().equals("messageId")){ message.setOpType(((JsonPrimitive) ot).getAsInt()); } } return message; } }).create();