package com.xxmassdeveloper.mpchartexample.manage;
import android.text.TextUtils;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.json.JSONObject;
/**
* @Info Gson的类操作
* @Auth Bello
* @Time 16-6-27 下午3:31
* @Ver
*/
public class JsonUtils {
/**
* JSONObject 转 class
* @param response
* @param classOfT
* @return
*/
public static Object JsonObjToClass(JSONObject response, Class classOfT) {
try {
if (null == response){
return null;
}
Gson gson = new Gson();
return gson.fromJson(response.toString(), classOfT);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* JSONOString 转 class
*
* @param jsonStr
* @param classOfT
* @return
*/
public static Object JsonStrToClass(String jsonStr, Class classOfT){
try {
if (TextUtils.isEmpty(jsonStr))
return null;
Gson gson = new Gson();
return gson.fromJson(jsonStr, classOfT);
} catch (JsonSyntaxException e) {
e.printStackTrace();
return null;
}
}
/**
* 对象转成json字符串
*
* @param obj
* @return
*/
public static String objectToJsonStr(Object obj){
try {
if (null == obj){
return null;
}
Gson gson = new Gson();
return gson.toJson(obj).toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}