当前位置:首页 > 安卓源码 > 技术博客 >

分享一个Android开发中json数据的解析类

时间:2017-09-25 15:18 来源:互联网 作者:源码搜藏 浏览: 收藏 挑错 推荐 打印

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 Jso 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();
	}
分享一个Android开发中json数据的解析类 转载https://www.codesocang.com/appboke/36629.html

技术博客阅读排行

最新文章