android解析json(以解析時时价为例),java也是一样的(二)

先把返回的json文档附上,以查询苹果为例:

"pager":{"total":"465","page":1,"rows":15},"products":[{"id":"8982","title":"\u82f9\u679c\uff08Apple\uff09iPad 2  MC769CH\/A  9.7\u82f1\u5bf8\u5e73\u677f\u7535\u8111 \uff0816G WIFI\u7248\uff09\u9ed1\u8272","url":"http:\/\/api.shishijia.com\/v1\/product\/8982","buyLink":"http:\/\/www.yihaodian.com\/product\/1114793_2","stock":"1","cover":{"small":"http:\/\/img14.360buyimg.com\/n4\/4815\/2183d9fa-d724-4abc-9e8c-8c7edd10bdd8.jpg","medium":"http:\/\/img14.360buyimg.com\/n2\/4815\/2183d9fa-d724-4abc-9e8c-8c7edd10bdd8.jpg","large":"http:\/\/img14.360buyimg.com\/n1\/4815\/2183d9fa-d724-4abc-9e8c-8c7edd10bdd8.jpg"},"lowPrice":"2825.00","highPrice":"3069.00","marketPrice":"3688.00","lowPriceStore":{"id":9,"storeKey":"yihaodian","storeName":"1\u53f7\u5e97","url":"http:\/\/www.yihaodian.com\/","enable":1,"supportAssociate":1,"classification":["3c","cosmetics"],"service":["genuineGuarantee","countryGuarantee","faultPay3","cashOnDelivery","payByPost","payByBank"],"fare":[],"fareUrl":"http:\/\/www.yihaodian.com\/deliveryinfo\/deliveryInfo.do"}},{"id":"1269964","title":"\u82f9\u679c\uff08APPLE\uff09iPhone 4S 16G\u7248 3G\u624b\u673a\uff08\u9ed1\u8272\uff09WCDMA\/GSM 0\u5143\u8d2d\u673a\u7248 \u4e0a\u6d77\u7279\u4ef7\u7248","url":"http:\/\/api.shishijia.com\/v1\/product\/1269964","buyLink":"http:\/\/www.360buy.com\/product\/640467.html","stock":"1","cover":{"small":"http:\/\/img12.360buyimg.com\/n4\/2643\/dd45d8c2-527a-4ce7-ba14-456b8a41d688.jpg","medium":"http:\/\/img12.360buyimg.com\/n2\/2643\/dd45d8c2-527a-4ce7-ba14-456b8a41d688.jpg","large":"http:\/\/img12.360buyimg.com\/n1\/2643\/dd45d8c2-527a-4ce7-ba14-456b8a41d688.jpg"},"lowPrice":"5180.00","highPrice":"5180.00","marketPrice":"5180.00","lowPriceStore":{"id":2,"storeKey":"jingdong","storeName":"\u4eac\u4e1c\u5546\u57ce","url":"http:\/\/www.360buy.com\/","enable":1,"supportAssociate":1,"classification":["3c","cosmetics"],"service":["freeShippingUnder39","timeLimitedShipping211","genuineGuarantee","priceProtect","extendGuarantee","replacement","installment","cashOnDelivery","payByPost"],"fare":[{"moreThan":0,"lessThan":39,"price":5},{"moreThan":39,"lessThan":"","price":0}],"fareUrl":"http:\/\/help.360buy.com\/help\/question-65.html"}},{"id":"8960","title":"\u82f9\u679c\uff08Apple\uff09MC940 iPad 2 Dock","url":"http:\/\/api.shishijia.com\/v1\/product\/8960","buyLink":"http:\/\/www.360buy.com\/product\/394499.html","stock":"1","cover":{"small":"http:\/\/img14.360buyimg.com\/n4\/4343\/b68b4316-5a33-492c-b36a-2690bfda5bfe.jpg","medium":"http:\/\/img14.360buyimg.com\/n2\/4343\/b68b4316-5a33-492c-b36a-2690bfda5bfe.jpg","large":"http:\/\/img14.360buyimg.com\/n1\/4343\/b68b4316-5a33-492c-b36a-2690bfda5bfe.jpg"},"lowPrice":"198.00","highPrice":"198.00","marketPrice":"198.00","lowPriceStore":{"id":2,"storeKey":"jingdong","storeName":"\u4eac\u4e1c\u5546\u57ce","url":"http:\/\/www.360buy.com\/","enable":1,"supportAssociate":1,"classification":["3c","cosmetics"],"service":["freeShippingUnder39","timeLimitedShipping211","genuineGuarantee","priceProtect","extendGuarantee","replacement","installment","cashOnDelivery","payByPost"],"fare":[{"moreThan":0,"lessThan":39,"price":5},{"moreThan":39,"lessThan":"","price":0}],"fareUrl":"http:\/\/help.360buy.com\/help\/question-65.html"}}

看起来比较乱,确实是这样的,看这个我就废了半个小时,这只是其中一部分

返回的数据主要是这样的,这个里面包括两大部分,一个是pager,一个是products,这个pager是个jsonObject,而products是个jsonArray,如果你看了我的地一篇文章,或是对json有点了解,那么很容易的就可以看出来。

下面是正式的解析部分:


首先是链接网络的部分:

 public String getJason(String url)throws Exception{
	   HttpClient client =new DefaultHttpClient();
	   HttpGet get=new HttpGet(url);
	   HttpResponse response=client.execute(get);
	   if(response.getStatusLine().getStatusCode()==200){
		   String result=EntityUtils.toString(response.getEntity());
		   return result;
	   }
	   
	return null;
	   
	   
   }

 这一部分是得到网络中你要解析的json的网络数据,返回的是字符串。

我所需要的只是products里面的数据,别的就不需要了,所以首先建立一个Bean类,product:

package com.test;

/**
 * @author sjy
 *
 * @date 2012-8-17下午7:07:27
 */
public class Product {
	private String id;
	private String title;
	private String url;
	private String buyLink;
	private String stock;
	private String coversmall;
	private String covermedium;
	private String coverlarge;
	private String lowPrice;
	private String highPrice;
	private String marketPrice;
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getBuyLink() {
		return buyLink;
	}
	public void setBuyLink(String buyLink) {
		this.buyLink = buyLink;
	}
	public String getStock() {
		return stock;
	}
	public void setStock(String stock) {
		this.stock = stock;
	}
	public String getCoversmall() {
		return coversmall;
	}
	public void setCoversmall(String coversmall) {
		this.coversmall = coversmall;
	}
	public String getCovermedium() {
		return covermedium;
	}
	public void setCovermedium(String covermedium) {
		this.covermedium = covermedium;
	}
	public String getCoverlarge() {
		return coverlarge;
	}
	public void setCoverlarge(String coverlarge) {
		this.coverlarge = coverlarge;
	}
	public String getLowPrice() {
		return lowPrice;
	}
	public void setLowPrice(String lowPrice) {
		this.lowPrice = lowPrice;
	}
	public String getHighPrice() {
		return highPrice;
	}
	public void setHighPrice(String highPrice) {
		this.highPrice = highPrice;
	}
	public String getMarketPrice() {
		return marketPrice;
	}
	public void setMarketPrice(String marketPrice) {
		this.marketPrice = marketPrice;
	}
	public String toString(){
		return id+url+buyLink+coversmall+covermedium+coverlarge+highPrice+lowPrice+marketPrice+stock+title;
	}
}

 这个便是需要解析获得的所有数据。

由于前面已经知道得到网络json的方法。那么下面便是正式解析json了:

首先要获得products这个数组:

 String s=url;
String  jsonString=“”;
       try {
		s=getJason(s);
		JSONObject  item=new JSONObject(s);
		JSONArray array=item.getJSONArray("products");
		
		
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
			
 这一步我们就可以获得products这个数组了。然后,我们对array里面的没一个object进行解析。

 

 

你可能感兴趣的:(java,android,json,json,java,移动开发)