json数据格式如下
{
"resultcode": "200",
"reason": "查询支持的快递公司成功",
"result": [
{
"com": "顺丰",
"no": "sf"
},
{
"com": "申通",
"no": "sto"
},
{
"com": "圆通",
"no": "yt"
},
{
"com": "韵达",
"no": "yd"
}
],
"error_code": 0
}
对我们有用的是result下的数据
新建一个javabean 记得加toString
public class ExpressVo {
private String com;
private String no;
/**
* @return the com
*/
public String getCom() {
return com;
}
/**
* @param com the com to set
*/
public void setCom(String com) {
this.com = com;
}
/**
* @return the no
*/
public String getNo() {
return no;
}
/**
* @param no the no to set
*/
public void setNo(String no) {
this.no = no;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ExpressVo [com=" + com + ", no=" + no + "]";
}
}
使用谷歌的json
String result = ExpressUtil.queryExpList(); //result为json数据
JsonParser parser = new JsonParser();
JsonObject object = (JsonObject) parser.parse(result);
JsonArray array = object.get("result").getAsJsonArray();
List<ExpressVo> list = new ArrayList<ExpressVo>();
for(int i=0;i<array.size();i++){
ExpressVo express = new ExpressVo();
JsonObject sub = array.get(i).getAsJsonObject();
express.setCom(sub.get("com").getAsString());
express.setNo(sub.get("no").getAsString());
list.add(express);
}
return list;
这样就可以使用jsp的<c:foreach>标签正常的遍历需要的数据