public class PraseUtils {

    /**
     * @param json 要解析的数据
     * @param classOfT 解析成的对象
     * @param ail  根节点名称ail
     * @return  返回解析成的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T parseJson(String json,Class<T> classOfT,String ail){
        Object object=null;
        Gson gson=new Gson();
        try {
            JSONObject jsonObject=new JSONObject(json);
            object=gson.fromJson(jsonObject.getString(ail), classOfT);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (T)object;
    }
    /**
     * 
     * @param json 要解析的数据
     * @param classOfT 解析成的对象
     * @return  返回解析成的对象
     */
    @SuppressWarnings("unchecked")
    public static <T> T parseJson(String json,Class<T> classOfT){
        Object object=null;
        Gson gson=new Gson();
        try {
            JSONObject jsonObject=new JSONObject(json);
            object=gson.fromJson(jsonObject.getString("res"), classOfT);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return (T)object;
    }
}

 

posted on 2014-08-25 13:54  青年程序猿  阅读(562)  评论(0)    收藏  举报