0
点赞
收藏
分享

微信扫一扫

jsonobject处理http请求返回结果的json

快乐小码农 2022-01-21 阅读 32
jsonhttpjava

采用hutool工具

<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.5</version>
</dependency>

//map中是参数
HashMap<String, Object> map = new HashMap<>();
//post请求
 String post = HttpUtil.post(url, map);
//转换JSONObject 
 JSONObject jsonObject = new JSONObject(post);
 //获取其中一个jsonstring
 String code = jsonObject.getStr("code");
 //获取其中的jsonobject  在获取其中的一条属性
 tonken =  jsonObject.getJSONObject("obj").getStr("accessToken");
 //转换list    
 //elementType :  xxx.class
 //key : 在json中的是list集合的属性名字
 List<T> rows = JSONUtil.toList(obj.getJSONArray(key), elementType);
//参数
 JSONObject param= new JSONObject();
        param.set("code", code);
        param.set("page", page);
        param.set("size", size);
//请求结果
 String result = HttpRequest.get(url)
 				//请求头中的参数
                .header("api-key", tonken)
                .form(param)
                .execute().body();
举报

相关推荐

0 条评论