核心功能增强 - 数据和工具类修改
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.miraclegarden.smsmessage;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
@@ -11,11 +12,12 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* json解析工具类 其实对于数组解析有一些问题
|
||||
* @author
|
||||
* json解析工具类
|
||||
* @author
|
||||
*/
|
||||
public class GsonUtils {
|
||||
|
||||
private static final String TAG = "GsonUtils";
|
||||
public static Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
@@ -27,7 +29,12 @@ public class GsonUtils {
|
||||
*/
|
||||
public static <T> T getListFromJSON(String str, Type type) {
|
||||
if (!TextUtils.isEmpty(str)) {
|
||||
return gson.fromJson(str, type);
|
||||
try {
|
||||
return gson.fromJson(str, type);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getListFromJSON(Type) parse error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -39,17 +46,26 @@ public class GsonUtils {
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> List<T> getListFromJSON(String str, Class<T> cls)
|
||||
{
|
||||
Type type = new TypeToken<ArrayList<JsonObject>>()
|
||||
{}.getType();
|
||||
ArrayList<JsonObject> jsonObjects = gson.fromJson(str, type);
|
||||
ArrayList<T> arrayList = new ArrayList<>();
|
||||
for (JsonObject jsonObject : jsonObjects)
|
||||
{
|
||||
arrayList.add(gson.fromJson(jsonObject, cls));
|
||||
public static <T> List<T> getListFromJSON(String str, Class<T> cls) {
|
||||
if (TextUtils.isEmpty(str)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
try {
|
||||
Type type = new TypeToken<ArrayList<JsonObject>>()
|
||||
{}.getType();
|
||||
ArrayList<JsonObject> jsonObjects = gson.fromJson(str, type);
|
||||
if (jsonObjects == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
ArrayList<T> arrayList = new ArrayList<>();
|
||||
for (JsonObject jsonObject : jsonObjects) {
|
||||
arrayList.add(gson.fromJson(jsonObject, cls));
|
||||
}
|
||||
return arrayList;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getListFromJSON(Class) parse error", e);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
return arrayList;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,11 +78,11 @@ public class GsonUtils {
|
||||
public static <T> T getObjFromJSON(String str, Class<T> cls) {
|
||||
try {
|
||||
if (!TextUtils.isEmpty(str)) {
|
||||
// LogUtils.i("参数:"+str);
|
||||
return gson.fromJson(str, cls);
|
||||
}
|
||||
return null;
|
||||
}catch (Exception e) {
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "getObjFromJSON parse error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -76,7 +92,15 @@ public class GsonUtils {
|
||||
* @return
|
||||
*/
|
||||
public static String beanToJSONString(Object bean) {
|
||||
return new Gson().toJson(bean);
|
||||
if (bean == null) {
|
||||
return "";
|
||||
}
|
||||
try {
|
||||
return gson.toJson(bean);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "beanToJSONString error", e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user