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

Json解析之FastJson

时间:2014-07-12 23:38:31      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:android   json   解析   

1、FastJson介绍

FastJson是阿里巴巴提供的一个Json处理工具包,具有极快的性能,超越任其他的Java Json parser。使用起来非常方便。

2、例程

首先下载fastjson-1.1.27-android.jar包并导入工程中:

DataBean.java:

package com.heqiang.testactivity;

public class DataBean{
    private String name;
    private String url;
    private String summary;
    private long id;
    public DataBean() {
    }
    
    public DataBean(long id, String name, String url, String summary) {
    	this.id = id;
    	this.name = name;
    	this.url = url;
    	this.summary = summary;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getSummary() {
        return summary;
    }
    public void setSummary(String summary) {
		this.summary = summary;
    }

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}
}

ResponseBean.java:

package com.heqiang.testactivity;

public class ResponseBean {
	private int code;
	private String message;
	private Value value;

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public Value getValue() {
		return value;
	}

	public void setValue(Value value) {
		this.value = value;
	}

	public static class Value {
		private String data;
		private boolean flag;
		
		public Value() {
		}
		
		public Value(String data, boolean flag) {
			this.data = data;
			this.setFlag(flag);
		}

        public String getData() {
			return data;
		}

		public void setData(String data) {
			this.data = data;
		}

		public boolean isFlag() {
			return flag;
		}

		public void setFlag(boolean flag) {
			this.flag = flag;
		}
	}

}


解析函数:

	private void testFastJson(){
		String data = "{\"code\":200,\"message\":\"\",\"value\":{\"flag\":true,\"data\":[{\"url\":\"http://www.baidu.com\",\"id\":1,\"name\":\"百度\",\"summary\":\"搜索引擎\"},{\"url\":\"http://www.sina.com\",\"id\":2,\"name\":\"新浪\",\"summary\":\"门户网站\"},{\"url\":\"http://www.qq.com\",\"id\":3,\"name\":\"腾讯\",\"summary\":\"社交网络\"},{\"url\":\"http://www.taobao.com\",\"id\":4,\"name\":\"淘宝\",\"summary\":\"购物网站\"}]}}";

		ResponseBean reponse = JSON.parseObject(data, ResponseBean.class);
		if(reponse != null && reponse.getCode() == 200 && reponse.getValue() != null 
				&& reponse.getValue().getData() != null){
			List<DataBean> list = JSON.parseArray(reponse.getValue().getData(), DataBean.class);
			for (DataBean dataBean : list) {
				Log.d("Test", "name = "+dataBean.getName()+", url = "+dataBean.getUrl()+", id = "
						+dataBean.getId()+", summary = "+dataBean.getSummary());
			}
		}
	}

程序运行后打印如下:

name = 百度, url = http://www.baidu.com, id = 1, summary = 搜索引擎
name = 新浪, url = http://www.sina.com, id = 2, summary = 门户网站
name = 腾讯, url = http://www.qq.com, id = 3, summary = 社交网络
name = 淘宝, url = http://www.taobao.com, id = 4, summary = 购物网站


Json解析之FastJson,布布扣,bubuko.com

Json解析之FastJson

标签:android   json   解析   

原文地址:http://blog.csdn.net/heqiangflytosky/article/details/37659943

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