Android 简介json解析的使用

首先定义一个http获取数据的方法:

//用于联网获取数据,并将数据转换为String
public class HttpUtil {
	public static String get(String url) {
		 String result = "";
		HttpClient client = new DefaultHttpClient();
		HttpGet get=new HttpGet(url);
		
		HttpResponse response;
		try {
			response = client.execute(get);
			if(200==response.getStatusLine().getStatusCode()){
				InputStream in=response.getEntity().getContent();
				ByteArrayOutputStream baos=new ByteArrayOutputStream();
				
				byte[] buffer=new byte[1024];
				int len;
				while((len=in.read(buffer))!=-1){
					baos.write(buffer, 0, len);
				}
				baos.flush();
				in.close();
				baos.close();
				
				byte[] bt=baos.toByteArray();
				result=new String(bt,"utf-8");
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
}

然后创建一个子线程去获取数据,可以使用Thread+handler的方式操作UI,也可以使用AsyncTask进行处理.

本文采用AsyncTask的方式:

	private void getdata() {
		new AsyncTask<Void, Void, HotProduct>() {

			@Override
			protected HotProduct doInBackground(Void... params) {
				String json = HttpUtil.get(url);  //联网获取数据
				Gson gson = new Gson();  //创建Gson对象,用于json解析,需要gson.jar包
				HotProduct hotProduct = gson.fromJson(json, HotProduct.class);   //此处用定义的格式去解析,并将数据赋予相应的对象
				return hotProduct;
			}


		}.execute();

	}

联网获取到的数据如下:

{"productlist":[{"id":10000,"marketprice":1000,"name":"雅戈尔0","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10001,"marketprice":1001,"name":"雅1","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10002,"marketprice":1002,"name":"雅2","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10003,"marketprice":1003,"name":"雅3","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10004,"marketprice":1004,"name":"雅4","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10005,"marketprice":1005,"name":"雅5","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10006,"marketprice":1006,"name":"雅6","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10007,"marketprice":1007,"name":"雅7","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10008,"marketprice":1008,"name":"雅8","pic":"http://192.168.1.105:8080/EServer/images/image10.png","price":800},{"id":10009,"marketprice":1009,"name":"雅9","pic":"http://192.168.1.105:8080/EServer_D/images/image10.png","price":800}],"response":"hotproduct","list_count":10}

HotProduct类如下定义:

public class HotProduct {

	public ArrayList<NewProductList> productlist;
	public String response;
	public String list_count;
	public HotProduct() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "HotProduct [productlist=" + productlist + ", response="
				+ response + ", list_count=" + list_count + "]";
	}
}

NewProductList:
public class NewProductList {

	public String id;
	public String marketprice;
	public String name;
	public String pic;
	public String price;
	public NewProductList() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public String toString() {
		return "NewProductList [id=" + id + ", marketprice=" + marketprice
				+ ", name=" + name + ", pic=" + pic + ", price=" + price + "]";
	}

	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值