基于java和json开发项目,经常需要将json串转换为java集合对象或者反过来转换,那么下面的一个工具类就挺有用的。
package com.tr.geda.evm.action.util;
import java.lang.reflect.Type;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
public class JsonConverter {
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param list
* @return
* @throws JSONException
* @return JSONArray
* @throws
*/
public static JSONArray listOfEntityToJsonArray(List list) throws JSONException {
JSONArray array = new JSONArray();
Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).setDateFormat(
"yyyy-MM-dd HH:mm:ss").create();
for (Object object : list ) {
String jsonStr = gson.toJson(object);
JSONObject json = new JSONObject(jsonStr);
array.put(json);
}
return array;
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param map
* @return
* @throws JSONException
* @return JSONObject
* @throws
*/
public static JSONObject mapToJson(Map<String, Object> map) throws JSONException {
Type mapType = new TypeToken<Map<String, Object>>() {
}.getType();
Gson gson = new GsonBuilder().create();
String jsonStr = gson.toJson(map, mapType);
JSONObject json = new JSONObject(jsonStr);
for (String key : map.keySet() ) {
Object value = map.get(key);
if (value==null || value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof Date) {
continue;
} else if (value instanceof Map) {
json.put(key, mapToJson((Map<String, Object>) map.get(key)));
} else if (value instanceof List) {
List list = (List) map.get(key);
if (list.size() > 0) {
if (list.get(0) instanceof Map)
json.put(key, listOfMapToJsonArray((List<Map<String, Object>>) list));
else
json.put(key, listOfEntityToJsonArray(list));
}
} else {
json.put(key, entityToJson(map.get(key)));
}
}
return json;
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param json
* @return
* @return Map<String,String>
* @throws
*/
public static Map<String, String> jsonToMap(JSONObject json) {
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
Gson gson = new GsonBuilder().create();
Map<String, String> map = gson.fromJson(json.toString(), mapType);
return map;
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param array
* @return
* @throws JsonParseException
* @throws JSONException
* @return List<Map<String,String>>
* @throws
*/
public static List<Map<String, String>> jsonArrayToListOfMap(JSONArray array) throws JsonParseException,
JSONException {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Gson gson = new GsonBuilder().create();
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
for (int i = 0; i < array.length(); i++ ) {
Map<String, String> map = gson.fromJson(array.get(i).toString(), mapType);
list.add(map);
}
return list;
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param list
* @return
* @throws JSONException
* @return JSONArray
* @throws
*/
public static JSONArray listOfMapToJsonArray(List<Map<String, Object>> list) throws JSONException {
JSONArray array = new JSONArray();
Gson gson = new GsonBuilder().create();
Type mapType = new TypeToken<Map<String, String>>() {
}.getType();
for (Map<String, Object> map : list ) {
array.put(mapToJson(map));
}
return array;
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param <T>
* @param json
* @param classOfT
* @return
* @return T
* @throws
*/
public static <T> T jsonToEntity(String json, Class<T> classOfT) {
Gson gson = new GsonBuilder().create();
return gson.fromJson(json, classOfT);
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param object
* @return
* @throws JSONException
* @return JSONObject
* @throws
*/
public static JSONObject entityToJson(Object object) throws JSONException {
Gson gson = new GsonBuilder().registerTypeAdapter(Timestamp.class, new TimestampTypeAdapter()).setDateFormat(
"yyyy-MM-dd HH:mm:ss").create();
return new JSONObject(gson.toJson(object));
}
/**
* <p>Title: </p>
* <p>Description: TODO</p>
*
* @param <T>
* @param array
* @param classOfT
* @return
* @throws JSONException
* @return List<T>
* @throws
*/
public static <T> List<T> jsonArrayToEntityList(JSONArray array, Class<T> classOfT) throws JSONException {
List<T> list = new ArrayList<T>();
for (int i = 0; i < array.length(); i++ ) {
list.add(jsonToEntity(array.getString(i), classOfT));
}
return list;
}
}