/*Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
This source code is licensed under the Apache License Version 2.0.*/
package apijson;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.util.List;
/**é¿éFastJSONå°è£
ç±» 鲿¢è§£ææ¶å¼å¸¸
* @author Lemon
*/
public class JSON {
private static final String TAG = "JSON";
/**夿jsonæ ¼å¼æ¯å¦æ£ç¡®
* @param s
* @return
*/
public static boolean isJsonCorrect(String s) {
//å¤ªé¿ Log.i(TAG, "isJsonCorrect <<<< " + 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 s
* @return
*/
public static String getCorrectJson(String s) {
return getCorrectJson(s, false);
}
/**è·åææçjson
* @param s
* @param isArray
* @return
*/
public static String getCorrectJson(String s, boolean isArray) {
s = StringUtil.getTrimedString(s);
// if (isArray) {
// while (s.startsWith("\"")) {
// s = s.substring(1);
// }
// while (s.endsWith("\"")) {
// s = s.substring(0, s.length() - 1);
// }
// }
return s;//isJsonCorrect(s) ? s : null;
}
/**
* @param json
* @return
*/
public static Object parse(Object obj) {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
try {
return com.alibaba.fastjson.JSON.parse(obj instanceof String ? (String) obj : toJSONString(obj), features);
} catch (Exception e) {
Log.i(TAG, "parse catch \n" + e.getMessage());
}
return null;
}
/**obj转JSONObject
* @param json
* @return
*/
public static JSONObject parseObject(Object obj) {
if (obj instanceof JSONObject) {
return (JSONObject) obj;
}
return parseObject(toJSONString(obj));
}
/**json转JSONObject
* @param json
* @return
*/
public static JSONObject parseObject(String json) {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
return parseObject(json, features);
}
/**json转JSONObject
* @param json
* @param features
* @return
*/
public static JSONObject parseObject(String json, int features) {
try {
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), JSONObject.class, features);
} catch (Exception e) {
Log.i(TAG, "parseObject catch \n" + e.getMessage());
}
return null;
}
/**JSONObject转å®ä½ç±»
* @param object
* @param clazz
* @return
*/
public static T parseObject(JSONObject object, Class clazz) {
return parseObject(toJSONString(object), clazz);
}
/**json转å®ä½ç±»
* @param json
* @param clazz
* @return
*/
public static T parseObject(String json, Class clazz) {
if (clazz == null) {
Log.e(TAG, "parseObject clazz == null >> return null;");
} else {
try {
int features = com.alibaba.fastjson.JSON.DEFAULT_PARSER_FEATURE;
features |= Feature.OrderedField.getMask();
return com.alibaba.fastjson.JSON.parseObject(getCorrectJson(json), clazz, features);
} catch (Exception e) {
Log.i(TAG, "parseObject catch \n" + e.getMessage());
}
}
return null;
}
/**list转JSONArray
* @param list
* @return
*/
public static JSONArray parseArray(List