1、Json转换成实体
Shop shop = JSONUtil.toBean(shopJson, Shop.class);
@SuppressWarnings("unchecked")
public static<T> T jsonToObject(String jsonString, Class<T> clazz){
if (clazz == String.class) {
return (T) jsonString;
} else {
return (T)gson.fromJson(jsonString, clazz);
}
}
2、实体转json字符串
String s = JSONUtil.toJsonStr(shopJson);
public static <T> String objectToJson(T t){
if (t instanceof String) {
return t.toString();
} else {
return gson.toJson(t);
}
}
3、list集合转换成json
public static<T> String listToJson(List<T> list){
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = null;
try {
for (int i = 0; i < list.size(); i++) {
jsonObject = new JSONObject(objectToJson(list.get(i)));
jsonArray.put(jsonObject);
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (jsonObject != null) {
jsonObject = null;
}
}
return jsonArray.toString();
}
4、数组转换成json
public static<T> String arrayToJson(T[] array){
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = null;
try {
for (int i = 0; i < array.length; i++) {
jsonObject = new JSONObject(objectToJson(array[i]));
jsonArray.put(jsonObject);
}
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (jsonObject != null) {
jsonObject = null;
}
}
return jsonArray.toString();
}
5、json字符串转换为ContentValues
@SuppressWarnings("rawtypes")
public static ContentValues jsonToContentValues(String json){
ContentValues contentValues = new ContentValues();
try {
JSONObject jsonObject = new JSONObject(json);
Iterator iterator = jsonObject.keys();
String key;
Object value;
while (iterator.hasNext()) {
key = iterator.next().toString();
value = jsonObject.get(key);
String valueString = value.toString();
if (value instanceof String) {
contentValues.put(key, valueString);
}else if(value instanceof Integer){
contentValues.put(key, Integer.valueOf(valueString));
}else if(value instanceof Long){
contentValues.put(key, Long.valueOf(valueString));
}else if(value instanceof Double){
contentValues.put(key, Double.valueOf(valueString));
}else if(value instanceof Float){
contentValues.put(key, Float.valueOf(valueString));
}else if(value instanceof Boolean){
contentValues.put(key, Boolean.valueOf(valueString));
}
}
} catch (JSONException e) {
e.printStackTrace();
throw new Error("Json字符串不合法:" + json);
}
return contentValues;
}
6、获取jsonObject对象中的值
@SuppressWarnings("unchecked")
public static<T> T getJsonObjectValue(JSONObject jsonObject, String key, Class<T> clazz){
T t = null;
try {
if (clazz == Integer.class) {
t = (T) Integer.valueOf(jsonObject.getInt(key));
}else if(clazz == Boolean.class){
t = (T) Boolean.valueOf(jsonObject.getBoolean(key));
}else if(clazz == String.class){
t = (T) String.valueOf(jsonObject.getString(key));
}else if(clazz == Double.class){
t = (T) Double.valueOf(jsonObject.getDouble(key));
}else if(clazz == JSONObject.class){
t = (T) jsonObject.getJSONObject(key);
}else if(clazz == JSONArray.class){
t = (T) jsonObject.getJSONArray(key);
}else if(clazz == Long.class){
t = (T) Long.valueOf(jsonObject.getLong(key));
}
} catch (JSONException e) {
e.printStackTrace();
}
return t;
}
7、获取json字符串中的值
public static<T> T getJsonObjectValue(String json, String key, Class<T> clazz){
try {
return getJsonObjectValue(new JSONObject(json), key, clazz);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}