Java教程

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);

上面打印的结果依次是:

 

这篇关于Java: 获取http请求response中某个值的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!