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

jsonobject处理http请求返回结果的json,第1张

jsonobject处理http请求返回结果的json 采用hutool工具

导入jar包


    cn.hutool
    hutool-all
    5.3.5


http发送请求.

//map中是参数
HashMap 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 rows = JSONUtil.toList(obj.getJSONArray(key), elementType);

http请求带有请求头

//参数
 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();

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5710254.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-17
下一篇2022-12-17

发表评论

登录后才能评论

评论列表(0条)

    保存