写了个工具类分享一下:
package com.mmzou.shareapp;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* GsonUtils.
*/
public class GsonUtils {
private static final Gson GSON = new Gson();
public static Gson getGson() {
return GSON;
}
/**
* Object->json.
*
* @param obj Object
* @return String
*/
public static String toJson(Object obj) {
return GSON.toJson(obj);
}
public static <T> T fromJson(String json, Class<T> cls) {
return GSON.fromJson(json, cls);
}
/**
* Json->ArrayList.
*/
public static <T> ArrayList<T> jsonToList(String json, Class<T> clz) {
Type type = new TypeToken<List<T>>() {}.getType();
return GSON.fromJson(json, type);
}
/**
* Json->ArrayList->HashMap.
*/
public static <T> ArrayList<HashMap<String, T>> toListMap(String json, Class<T> clz) {
Type type = new TypeToken<ArrayList<HashMap<String, T>>>() {}.getType();
return GSON.fromJson(json, type);
}
/**
* To solve the problem of converting json data to ArrayList Map,
* integer types will be replaced with floating point types.
*
* @param jsonData String
* @return ArrayList
*/
public static ArrayList<HashMap<String, Object>> toListMapNoChange(String jsonData) {
Gson gson = new GsonBuilder().registerTypeAdapter(new TypeToken<ArrayList<HashMap<String, Object>>>() {
}.getType(), (JsonDeserializer<ArrayList<HashMap<String, Object>>>) (json, typeOfT, context) -> {
ArrayList<HashMap<String, Object>> list = new ArrayList<>();
JsonArray asJsonArray = json.getAsJsonArray();
for (JsonElement jsonElement : asJsonArray) {
HashMap<String, Object> hashMap = new HashMap<>();
JsonObject asJsonObject = jsonElement.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = asJsonObject.entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
hashMap.put(entry.getKey(), entry.getValue());
}
list.add(hashMap);
}
return list;
}).create();
return TextUtils.isEmpty(jsonData) ? new ArrayList<>() : gson.fromJson(jsonData,
new TypeToken<ArrayList<HashMap<String, Object>>>() {
}.getType());
}
/**
* To solve the problem of converting json data to Map,
* integer types will be replaced with floating point types.
*
* @param strJson String
* @return HashMap
*/
public static HashMap<String, Object> toMapNoChange(String strJson) {
Gson gson = new GsonBuilder().registerTypeAdapter(new TypeToken<HashMap<String, Object>>() {
}.getType(), (JsonDeserializer<HashMap<String, Object>>) (json, typeOfT, context) -> {
HashMap<String, Object> hashMap = new HashMap<>();
JsonObject jsonObject = json.getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
hashMap.put(entry.getKey(), entry.getValue());
}
return hashMap;
}).create();
return TextUtils.isEmpty(strJson) ? new HashMap<>() : gson.fromJson(strJson,
new TypeToken<HashMap<String, Object>>() {
}.getType());
}
}