先把返回的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进行解析。
分享到:
相关推荐
在Android Studio中通过GET请求到的JSON对象的解析源码,每一步都有注释,方便小白萌新学习。
使用java来解析json
1. android解析jison的完整例子。 2. 解析的json在 http://live.eagleapp.tv/data/live.json 另外付有代码对应的live.json文件,如果链接失效或有变更请自己搭建服务器。 3. 解析出的结果使用本地网页做的一个...
解决当内存资源不充足时大数据量的JSON文本解析为JSON对象会导致内存溢出的问题。 基于org.json的部分源代码,以及新增的JsonLazyer类。 原理:用时创建的规则降低传统解析器一次性将文本转成Java对象而占用大量内存...
java&android;解析xml 将xml转换为json java&android;解析xml 将xml转换为json 原文地址:https://blog.csdn.net/u010838785/article/details/82258411
解析Json字符串直接转换为对象(包括多层List集合 嵌套)所用jar包
使用json.org.jar包,解析json文件,比较简洁明了,该资源是一个android小DEMO
1、生成Json数据 2、使用Gson解析Json数据(单个json对象、json数组和json对象内嵌json数组) // 这里有两篇好心人的博文,有详细的讲解 http://www.jb51.net/article/32547.htm ...
使用Java解析json前的准备工作,需要准备下面的六个jar包
java JSON 解析 保证生成的JSON是有序结构,代码取至Android源码。
4. android的apk给你说完成http数据请求和json获取解析。 5. 把index.php部署到你的php服务器. 6. 修改MainActivity.java中的 final private String DATA_URL = "http://192.168.1.5/test/index.php"; 指向你部署好...
Android 解析json_dome.zip源码资源下载Android 解析json_dome.zip源码资源下载
Android/ java 省市json (包括 香港 澳门 台湾)
解放你的双手,不用再去一个一个JSONObject去处理解析问题,两行代码搞定一切
Android 应用项目开发源码 参考和学习使用
通过编写配置文件,该代码生成器能自动生成相应的bean,和解析json的代码。 你需要做的是,有一个ruby环境,将配置文件写在test.conf中,然后shell的重定向即可。 如生成Bean文件: ruby BeanGen.rb > Bean.java 生成...
Android 解析json_dome.zip项目安卓应用源码下载Android 解析json_dome.zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
利用金山词霸开放性api,通过异步处理类访问网络 获取到json,再通过Gson解析json,获取到金山词霸 的每日一句数据.
json java 解析学习android的好资料
开发中遇到的复杂JSON,用TypeToken来进行处理:(当然还有第二种解析方式,请查看http://blog.csdn.net/jason_996/article/details/78039799) 处理方法: 1,先将接口返回的JSON,用gson.tojson(map)方法,将...