java - Processing the return parameters (PARSEOBJECT, GETJSONARRAY, GETJSONOBJECT)

A A result of Response returns, get the value of the field reserve3, the operation code is as follows:

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"
                + "}";
        //Convert the string to object
        JSONObject jo = JSON.parseObject(a);
        //Get the object of Data object Data is [], so the object to be converted to the Jsonarray type
        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);

        }

    }
}

 

# Get Header and 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 is:" +errcode);
        String errmsg = headerJsonObject.getString("errmsg");
        System.out.println("Errmsg is:" + Errmsg);

 

# The following link is better, and each step has an explanation.

https://www.cnblogs.com/chushujin/p/11371450.html