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

Gson解析json数据

时间:2015-04-22 11:47:56      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:android   gson   json   

转载请注明出处:http://blog.csdn.net/itachi85/article/details/45172205

        json解析我们不陌生了,不过每次编写解析的代码时总是很头疼,一堆很无聊的代码,如果有50个接口那就得写50个相似的解析处理。其实谷歌和阿里很早就推出了解析json的工具包,谷歌的Gson,阿里的FastJson,还有一个jackJson。有人说jackJson解析速度快,大数据时FastJson要比Gson效率高,小数据时反之。不过我还是偏爱谷歌的Gson,即使效率可能偏低,但是稳定性肯定没得说,FastJson和jackJson就不知道了。

     现在我们来看看Json是如何解析的:

1.下载Gson开发包,放入工程libs目录下:

http://download.csdn.net/detail/itachi85/8617065

2.编写Bean文件:

先看一下json数据:

{
    "array": [   
        {
            "username": "在中", 
            "recommend": 0, 
            "user_photo": "0", 
            "like": false, 
            "reply_cnt": 3, 
            "distance": "2154m", 
            "add_date": "3天前", 
            "text": "肯德基快吃吐了,明天去麦当劳看看", 
            "stamp__name": null, 
            "place_id": 1, 
            "ptype": "d", 
            "like_cnt": "2", 
            "posting_image_url": "1", 
            "stamp_name": null, 
            "user": 0, 
            "stamp__id": null, 
            "dislike": "0", 
            "icon": "cate_icon_a_09", 
            "id": 0, 
            "mayor_cnt": 2, 
            "place_name": "东大桥肯德基",
	    "place_level": 2
        }, 
        {
            "username": "在中", 
            "recommend": 0, 
            "user_photo": "0", 
            "like": false, 
              "reply_cnt": 0, 
            "distance": "2425m", 
            "add_date": "3天前", 
            "text": "这是在跳僵尸舞呢吗?", 
            "stamp__name": null, 
            "place_id": 7, 
            "ptype": "d", 
            "like_cnt": "0", 
            "posting_image_url": "0", 
            "stamp_name": null, 
            "user": 0, 
            "stamp__id": null, 
            "dislike": "0", 
            "icon": "cate_icon_b_05", 
            "id": 4, 
            "mayor_cnt": 2, 
            "place_name": "悠唐",
	    "place_level": 1
        } 
    ], 
    "error": 0, 
    "next": true
}
       

紧接着,我们创建bean文件

需要注意的是:

   1、内部嵌套的类必须是static修饰的;
   2、类里面的属性名必须跟Json字段里面的Key是一样的;


NearDynamic.java:

public class NearDynamic {
	private Boolean next=false;
	private  int error;
	private ArrayList<Dynamic> array = new ArrayList<Dynamic>();
	public Boolean getNext() {
		return next;
	}
	public void setNext(Boolean next) {
		this.next = next;
	}
	public int getError() {
		return error;
	}
	public void setError(int error) {
		this.error = error;
	}
	public ArrayList<DynamicJ> getArray() {
		return array;
	}
	public void setArray(ArrayList<DynamicJ> array) {
		this.array = array;
	}
}


上面包含了error,next字段,以及包含类型是Dynamic的List,接着我们创建Dynamic:

public class Dynamic {
	private String username;
	private int recommend;
	private String user_photo;
	private Boolean like;
	private int replycnt;
	private String distance;
	private String add_date;
	private String text;
	private String stamp__name;
	private int place_id;
	private int like_cnt; 
	private String ptype;	
	private String posting_image_url;
	private String stamp_name;
	private int user;
	private String stamp__id;
	private int dislike=0;
	private String icon;
	private int id;
	private String place_name;
	private int place_level;
	private String mayor_cnt;
	public String getStamp__name() {
		return stamp__name;
	}
	public void setStamp__name(String stamp__name) {
		this.stamp__name = stamp__name;
	}
	public int getLike_cnt() {
		return like_cnt;
	}
	public void setLike_cnt(int like_cnt) {
		this.like_cnt = like_cnt;
	}
	public String getStamp_name() {
		return stamp_name;
	}
	public void setStamp_name(String stamp_name) {
		this.stamp_name = stamp_name;
	}
	public String getStamp__id() {
		return stamp__id;
	}
	public void setStamp__id(String stamp__id) {
		this.stamp__id = stamp__id;
	}
	public int getDislike() {
		return dislike;
	}
	public void setDislike(int dislike) {
		this.dislike = dislike;
	}
	public String getMayor_cnt() {
		return mayor_cnt;
	}
	public void setMayor_cnt(String mayor_cnt) {
		this.mayor_cnt = mayor_cnt;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getUser() {
		return user;
	}
	public void setUser(int user) {
		this.user = user;
	}
	public String getUsername() {
		return username;
	}
	...	
}

3.使用Gson解析数据:

	private int parseUserData() {
		 String strContent = Utils.getJson("neardynamic.txt");
		if (!TextUtils.isEmpty(strContent)) {
			try {
				Gson mgson = new Gson();
				NearDynamic mNearDynamic = mgson.fromJson(
						strContent, NearDynamic.class);
				ArrayList<Dynamic> mNearDynamicList = mNearDynamic
						.getArray();
				LogController.i(mNearDynamicList.get(1).getUsername());
				return BaseEngine.RESULT_OK;
			} catch (Exception e) {
				e.printStackTrace();
				return BaseEngine.PARSEJSON_ERR;
			}
		}
		return BaseEngine.RESULT_ERR;
	}


上面代码是一个解析的方法:直接从txt文件中读取数据由Json解析,首先由Json生成NearDynamic,然后得到array中的列表数据,我们取第二个数据打下log,得出的结果是"在中",解析完毕。

如果想要快速测试的话,就写一个txt文件,里面写有json数据,放入assets文件中,然后用下面的方法读取txt文件中数据,再按照上面所讲的就可以了:

	public static String getJson(String filename) {
		InputStream mInputStream = null;
		String resultString = "";
		try {
			mInputStream = App.mContext.getAssets().open(filename);
			byte[] buffer = new byte[mInputStream.available()];
			mInputStream.read(buffer);
			resultString = new String(buffer, "GB2312");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				mInputStream.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return resultString.toString();
	}

想要了解Gson更多的方法可以下载http://download.csdn.net/detail/itachi85/8617065中的文件,里面有Gson的文档。






























Gson解析json数据

标签:android   gson   json   

原文地址:http://blog.csdn.net/itachi85/article/details/45172205

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