public class ResolveJsonUtils {
public static <T> T getJson(String response, Class<T> cls)
throws JsonSyntaxException {
return new Gson().fromJson(response, cls);
}
public static <T> T getJson(String response, TypeToken<T> type)
throws JsonSyntaxException {
return new Gson().fromJson(response, type.getType());
}
public static String setJson(Object obj) {
return new Gson().toJson(obj);
}
/**
* @param response 需要第一层解析的数据
* @param params 想要解析第一层数据的哪一节点
* @throws JSONException
*/
public static String getDataJson(String response, String params)
throws JSONException {
JSONObject jsonObject = new JSONObject(response);
// return (String) jsonObject.get(params);
return jsonObject.get(params).toString();
}
}// 调用的方法
try {
// 直接解析第一层 user_id是json字符串中的参数名称
user_id = ResolveJsonUtils.getDataJson(response, "user_id");
} catch (JSONException e) {
e.printStackTrace();
}
點擊查看更多內容
為 TA 點贊
評論
評論
共同學習,寫下你的評論
評論加載中...
作者其他優質文章
正在加載中
感謝您的支持,我會繼續努力的~
掃碼打賞,你說多少就多少
贊賞金額會直接到老師賬戶
支付方式
打開微信掃一掃,即可進行掃碼打賞哦