• 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 = "{
    "
                    + " "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

  • 相关阅读:
    Eclipse遇到Initializing Java Tooling终极解决办法
    Quartz定时任务框架理解
    Quartz定时任务调度
    jQueryEasyUI Messager基本使用
    转一篇关于Log4j的介绍文章,很详细
    android联系人
    手机中预置联系人(姓名,号码,头像)
    android获取硬件信息
    Sumatra pdf更改起始页面的黄色颜色
    Eclipse背景颜色设置(设置成豆沙绿色保护眼睛,码农保护色)
  • 原文地址:https://www.cnblogs.com/eosclover/p/15403412.html
Copyright © 2020-2023  润新知