Java-对返回参数进行处理(parseObject,getJSONArray,getJSONObject)

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 = "{\n"
                + " \"header\": {\n"
                + "  \"errcode\": \"0000000000\",\n"
                + "  \"errmsg\": \"SUCCESS\"\n"
                + " },\n"
                + " \"data\": [{\n"
                + "  \"idDynBizSd\": \"20\",\n"
                + "  \"sdBiz\": \"2\",\n"
                + "  \"cdDynBizSd\": \"201\",\n"
                + "  \"nmDynBizSd\": \"ww\",\n"
                + "  \"cdDynBizSdPar\": \"0\",\n"
                + "  \"reserve1\": \"qq\",\n"
                + "  \"reserve2\": \"ww\",\n"
                + "  \"reserve3\": \"reserve3:1\",\n"
                + "  \"reserve4\": null,\n"
                + "  \"reserve5\": null\n"
                + " },\n"
                + " {\n"
                + "  \"idDynBizSd\": \"20\",\n"
                + "  \"sdBiz\": \"2\",\n"
                + "  \"cdDynBizSd\": \"201\",\n"
                + "  \"nmDynBizSd\": \"ww\",\n"
                + "  \"cdDynBizSdPar\": \"0\",\n"
                + "  \"reserve1\": \"qq\",\n"
                + "  \"reserve2\": \"ww\",\n"
                + "  \"reserve3\": \"reserve3:2\",\n"
                + "  \"reserve4\": null,\n"
                + "  \"reserve5\": null\n"
                + " },\n"
                + " {\n"
                + "  \"idDynBizSd\": \"20\",\n"
                + "  \"sdBiz\": \"2\",\n"
                + "  \"cdDynBizSd\": \"201\",\n"
                + "  \"nmDynBizSd\": \"ww\",\n"
                + "  \"cdDynBizSdPar\": \"0\",\n"
                + "  \"reserve1\": \"qq\",\n"
                + "  \"reserve2\": \"ww\",\n"
                + "  \"reserve3\": \"reserve3:3\",\n"
                + "  \"reserve4\": null,\n"
                + "  \"reserve5\": null\n"
                + " }]\n"
                + "}";
        //将字符串转换为了对象
        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

posted @ 2021-10-14 10:48  eosclover  Views(592)  Comments(0Edit  收藏  举报