package cn.face.faceDetection.util;
import android.util.Log;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
public class JsonUtils {
private static final String TAG = "JSON";
/**
* 判斷json格式是否正確
*
* @param s
* @return
*/
public static boolean isJsonCorrect(String s) {
if (s == null || s.equals("[]")
|| s.equals("{}") || s.equals("") || s.equals("[null]") || s.equals("{null}") || s.equals("null")) {
return false;
}
return true;
}
/**
* 獲取有效的json
*
* @param json
* @return
*/
public static String getCorrectJson(String json) {
return isJsonCorrect(json) ? json : "";
}
/**
* @param obj
* @return
*/
public static JSONObject parseObject(Object obj) {
return parseObject(toJSONString(obj));
}
/**
* json轉JSONObject
*
* @param json
* @return
*/
public static JSONObject parseObject(String json) {
try {
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json));
} catch (Exception e) {
Log.e(TAG, "parseObject catch \n" + e.getMessage());
}
return null;
}
/**
* JSONObject轉實體類
*
* @param object
* @param clazz
* @return
*/
public static <T> T parseObject(JSONObject object, Class<T> clazz) {
return parseObject(toJSONString(object), clazz);
}
/**
* json轉實體類
*
* @param json
* @param clazz
* @return
*/
public static <T> T parseObject(String json, Class<T> clazz) {
try {
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz);
} catch (Exception e) {
Log.e(TAG, "parseObject catch \n" + e.getMessage());
}
return null;
}
/**
* @param obj
* @return
*/
public static String toJSONString(Object obj) {
if (obj instanceof String) {
return (String) obj;
}
try {
return com.alibaba.fastjson.JSON.toJSONString(obj);
} catch (Exception e) {
Log.e(TAG, "toJSONString catch \n" + e.getMessage());
}
return null;
}
/**
* @param json
* @return
*/
public static JSONArray parseArray(String json) {
try {
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json));
} catch (Exception e) {
Log.e(TAG, "parseArray catch \n" + e.getMessage());
}
return null;
}
/**
* @param json
* @param clazz
* @return
*/
public static <T> List<T> parseArray(String json, Class<T> clazz) {
try {
return com.alibaba.fastjson.JSON.parseArray(getCorrectJson(json), clazz);
} catch (Exception e) {
Log.e(TAG, "parseArray catch \n" + e.getMessage());
}
return null;
}
/**
* 格式化,顯示更好看
*
* @param object
* @return
*/
public static String format(Object object) {
try {
return com.alibaba.fastjson.JSON.toJSONString(object, true);
} catch (Exception e) {
LogUtils.i("format catch \n" + e.getMessage());
}
return null;
}
/**
* 判斷是否爲JSONObject
*
* @param obj instanceof String ? parseObject
* @return
*/
public static boolean isJSONObject(Object obj) {
if (obj instanceof JSONObject) {
return true;
}
if (obj instanceof String) {
try {
JSONObject json = parseObject((String) obj);
return json != null && json.isEmpty() == false;
} catch (Exception e) {
Log.e(TAG, "isJSONObject catch \n" + e.getMessage());
}
}
return false;
}
/**
* 判斷是否爲JSONArray
*
* @param obj instanceof String ? parseArray
* @return
*/
public static boolean isJSONArray(Object obj) {
if (obj instanceof JSONArray) {
return true;
}
if (obj instanceof String) {
try {
JSONArray json = parseArray((String) obj);
return json != null && json.isEmpty() == false;
} catch (Exception e) {
Log.e(TAG, "isJSONArray catch \n" + e.getMessage());
}
}
return false;
}
}
【Android】json工具類[JsonUtil]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.