a代表response返回的结果,获取字段reserve3的值,操作代码如下:
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public class testBeanshellAss { public static void main(String[] args) { String a = "{ " + " "header": { " + " "errcode": "0000000000", " + " "errmsg": "SUCCESS" " + " }, " + " "data": [{ " + " "idDynBizSd": "20", " + " "sdBiz": "2", " + " "cdDynBizSd": "201", " + " "nmDynBizSd": "ww", " + " "cdDynBizSdPar": "0", " + " "reserve1": "qq", " + " "reserve2": "ww", " + " "reserve3": "reserve3:1", " + " "reserve4": null, " + " "reserve5": null " + " }, " + " { " + " "idDynBizSd": "20", " + " "sdBiz": "2", " + " "cdDynBizSd": "201", " + " "nmDynBizSd": "ww", " + " "cdDynBizSdPar": "0", " + " "reserve1": "qq", " + " "reserve2": "ww", " + " "reserve3": "reserve3:2", " + " "reserve4": null, " + " "reserve5": null " + " }, " + " { " + " "idDynBizSd": "20", " + " "sdBiz": "2", " + " "cdDynBizSd": "201", " + " "nmDynBizSd": "ww", " + " "cdDynBizSdPar": "0", " + " "reserve1": "qq", " + " "reserve2": "ww", " + " "reserve3": "reserve3:3", " + " "reserve4": null, " + " "reserve5": null " + " }] " + "}"; //将字符串转换为了对象 JSONObject jo = JSON.parseObject(a); //获取data对象 data的对象为[],所以要转化为JSONArray类型的对象 JSONArray data = jo.getJSONArray("data"); int size = data.size(); for(int i = 0 ; i<size ; i++) { JSONObject dataIndex = data.getJSONObject(i); String reserve3 = dataIndex.getString("reserve3"); System.out.println(reserve3); } } }
#获取header及errcode、errmsg
JSONObject resultJsonObject = JSONObject.parseObject(a); JSONObject headerJsonObject = resultJsonObject.getJSONObject("header"); System.out.println(headerJsonObject); String errcode = headerJsonObject.getString("errcode"); System.out.println("errcode为:"+errcode); String errmsg = headerJsonObject.getString("errmsg"); System.out.println("errmsg为:"+errmsg);
#如下链接的文章,写的比较好,每一步骤都有说明。
https://www.cnblogs.com/chushujin/p/11371450.html