package util.config;
import https://www.liuwanlin.info/files/java.util.arraylist;
import https://www.liuwanlin.info/files/java.util.hashmap;
import https://www.liuwanlin.info/files/java.util.list;
import https://www.liuwanlin.info/files/java.util.map;
import https://www.liuwanlin.info/files/com.alibaba.fastjson.jsonarray;
import https://www.liuwanlin.info/files/com.alibaba.fastjson.jsonobject;
/**
* json格式转化
*/
public final class JSONFormatter {
/**
* JSONObject转为map
* @param object json对象
* @return 转化后的Map
*/
public static Map<String, Object> toMap(JSONObject object){
Map<String, Object> map = new HashMap<String, Object>();
for (String key : object.keySet()) {
Object value = object.get(key);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
map.put(key, value);
}
return map;
}
/**
* JSONArray转为List
* @param array json数组
* @return 转化后的List
*/
public static List<Object> toList(JSONArray array){
List<Object> list = new ArrayList<Object>();
for(int i = 0; i < array.size(); i++) {
Object value = array.get(i);
if(value instanceof JSONArray) {
value = toList((JSONArray) value);
}
else if(value instanceof JSONObject) {
value = toMap((JSONObject) value);
}
list.add(value);
}
return list;
}
}