码迷,mamicode.com
首页 > Web开发 > 详细

Json解析工具类JsonHelper

时间:2015-03-10 13:57:19      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:工具   json   android   gson   解析   

利用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]]

Json解析工具类JsonHelper

标签:工具   json   android   gson   解析   

原文地址:http://blog.csdn.net/u014580558/article/details/44174429

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!