Json解析工具类JsonHelper

利用Gson快速解析json

需要导入Gson包,解析成一个对象或一个包含此对象的list

JsonHelper代码如下:

import java.util.ArrayList;
import java.util.List;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

public class JsonHelper {
    private static JsonHelper util;

    public static JsonHelper getInstance() {

        if (util == null) {
            util = new JsonHelper();

        }
        return util;

    }
    private JsonHelper() {
        super();
    }

    public String createJsonString(Object value) {
        Gson gson = new Gson();
        String str = gson.toJson(value);
        return str;
    }

    public <T> T getObject(String jsonString, Class<T> cls) {
        T t = null;
        try {
            if (isGoodJson(jsonString)) {
                Gson gson = new Gson();
                t = gson.fromJson(jsonString, cls);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }

    public <T> List<T> getList(String jsonString, TypeToken<T> tt) {
        List<T> list = new ArrayList<T>();
        try {
            if (isGoodJson(jsonString)) {
                Gson gson = new Gson();

                list = gson.fromJson(jsonString, tt.getType());
            }

        } catch (Exception e) {
        }
        return list;
    }

    public boolean isBadJson(String json) {
        return !isGoodJson(json);
    }

    public boolean isGoodJson(String json) {
        if (TextUtils.isEmpty(json)) {
            return false;
        }
        try {
            new JsonParser().parse(json);

        } catch (JsonParseException e) {
            return false;
        }

        return true;
    }

}

使用方法

Bean bean = new Bean();
        bean.age="30";
        bean.name="name";
        String result = JsonHelper.getInstance().createJsonString(bean);
        System.out.println(result);
        Bean bean2 = JsonHelper.getInstance().getObject(result, Bean.class);
        System.out.println(bean2.toString());


        ArrayList<Bean> list = new ArrayList<Bean>();
        list.add(bean);
        list.add(bean2);
        result = JsonHelper.getInstance().createJsonString(list);
        System.out.println(result);
        List<ArrayList<Bean>> list2 = JsonHelper.getInstance().getList(result, new TypeToken<ArrayList<Bean>>(){});
        System.out.println(list2.toString());

Bean.java

public class Bean {

    String name="";
    String age="";
    @Override
    public String toString() {
        return "Bean [name=" + name + ", age=" + age + "]";
    }



}

打印结果:

03-10 13:47:09.291: I/System.out(22869): {"age":"30","name":"name"}
03-10 13:47:09.291: I/System.out(22869): Bean [name=name, age=30]
03-10 13:47:09.301: I/System.out(22869): [{"age":"30","name":"name"},{"age":"30","name":"name"}]
03-10 13:47:09.301: I/System.out(22869): [Bean [name=name, age=30], Bean [name=name, age=30]]

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。