java获取返回的json值_fastjson提取json返回值(java)

接口调用返回的json,要提取后入库到本地。

返回json数据:

{

"data": {

"offset": 2,

"total": 2952,

"restTotal": 2950,

"dataList": [{

"ISBN": "9787539197456",

"bookStar": "93.6",

"tag": "",

"commentTimes": "1487",

"price": "58.00",

"star50Percent": "",

"star40Percent": "",

"star30Percent": "",

"star20Percent": "",

"star10Percent": ""

}, {

"ISBN": "9787539197449",

"bookStar": "95.8",

"tag": "",

"commentTimes": "15711",

"price": "25.00",

"star50Percent": "",

"star40Percent": "",

"star30Percent": "",

"star20Percent": "",

"star10Percent": ""

}]

},

"error": "success",

"error_Description": "操作成功"

}

使用fastjson取值:

importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;

{

JSONObject json_o= GetJson(调用接口的方法,得到返回值);//方法就是一个Httpclient的get//得到data的json对象

JSONObject jsondata = JSON.parseObject(json_o.getString("data"));//得到data中的dataList对象,并用jsonarray保存

JSONArray jsonarr = jsondata.getJSONArray("dataList");//使用for循环遍历datalist里的json值,如:price,ISBN

for(int i = 0;i

JSONObject jsonb=jsonarr.getJSONObject(i);

String price= jsonb.getString("price");

String ISBN= jsonb.getString("ISBN");//其他值的获取相同.取到值后再入库

}//获取total值

String total = jsondata.getString("total");//获取error值

String error = json_o.getString("error"));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值