zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    windows文件名非法字符过滤检测-正则表达式
    IEflash遇到flash遮挡
    打印iphone支持的所有字体
    xml字符串转xml对象,xml对象转json对象
    本地windows安装memcached服务
    resin启动时报错com.caucho.config.LineConfigException的解决
    8个Javascript小技巧,让你写的代码有腔调
    如何在MySQl数据库中给已有的数据表添加自增ID?
    mysql删除重复数据方法
    用Rem来无脑还原Web移动端自适应的页面
  • 原文地址:https://www.cnblogs.com/eosclover/p/15403412.html
Copyright © 2011-2022 走看看