编程语言
首页 > 编程语言> > Java: 获取http请求response中某个值

Java: 获取http请求response中某个值

作者:互联网

需要做的:  获取http请求response-body 中某个值

具体思路:

(1)执行 http请求

(2)将请求响应转换成json格式,后续想获取某个值直接用json格式获取即可

        //CommonHttpReq是自己封装的http请求类,返回的是response-body
        String resoust= CommonHttpReq.postReqStr(expsaveBodystr,saveheaderurl,header);
        System.out.println(resoust);

        //将resoust转换成jsonPath 格式
        io.restassured.path.json.JsonPath jsonPath 
                            =io.restassured.path.json.JsonPath.from(resoust);

        //以下就是json提取字段的格式,超简单
        String rowsdata=jsonPath.get("rows").toString();
        String rows0data=jsonPath.get("rows[0]").toString();
        String reExpReportHeaderId=jsonPath.get("rows[0].reExpReportHeaderId").toString();
        String expReportNumber=jsonPath.get("rows[0].expReportNumber").toString();

        System.out.println(rowsdata);
        System.out.println(rows0data);
        System.out.println(reExpReportHeaderId);
        System.out.println(expReportNumber);

上面打印的结果依次是:

 

标签:http,String,System,json,jsonPath,println,Java,response,out
来源: https://blog.csdn.net/gzl0524/article/details/122823692