发布新版本1.0.28-SNAPSHOT

修改utils包结构
This commit is contained in:
董宏宇
2021-02-22 14:32:07 +08:00
parent 88145786de
commit e30e9cab0e
38 changed files with 48 additions and 57 deletions

View File

@@ -0,0 +1,79 @@
package com.mogo.cloud;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class GsonUtil {
private static volatile Gson gson;
private GsonUtil() {}
public static Gson getGson() {
if(gson == null){
synchronized (GsonUtil.class) {
if (gson == null) {
GsonBuilder builder = new GsonBuilder();
gson = builder.create();
}
}
}
return gson;
}
public static String jsonFromObject( Object object) {
if (object == null) {
return null;
} else {
try {
return getGson().toJson(object);
} catch ( Exception var2) {
var2.printStackTrace();
return null;
}
}
}
public static <T> T objectFromJson( String json, Class<T> klass) {
if (json == null) {
return null;
} else {
try {
return getGson().fromJson(json, klass);
} catch ( Exception var3) {
var3.printStackTrace();
return null;
}
}
}
public static <T> List<T> arrayFromJson( String json, Class<T> clazz) {
List<T> list = new ArrayList<T>();
if ( TextUtils.isEmpty(json)) {
return null;
}
try {
org.json.JSONArray array = new org.json.JSONArray(json);
for (int i = 0; i < array.length(); i++) {
JSONObject object = array.getJSONObject(i);
String js = object.toString();
T t = GsonUtil.objectFromJson(js, clazz);
list.add(t);
}
return list;
} catch ( Exception e) {
e.printStackTrace();
}
return null;
}
}