gpt4 book ai didi

java - 如何从 Java 中的 Unirest 响应中获取原始 JSON 文本

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:37:45 25 4
gpt4 key购买 nike

我正在尝试向服务器发送 POST 请求、获取响应并解析它(它是一个 JSON 文件)。

我正在为我的 POST 请求使用 Unirest,如下所示:

        HttpResponse<JsonNode> response = Unirest
.post("http://myserver.com/file")
.header("cache-control", "no-cache")
.header("Postman-Token", "02ec2fa1-afdf-4a2a-a535-353424d99400")
.header("Content-Type", "application/json")
.body("{some JSON body}")
.asJson();

// retrieve the parsed JSONObject from the response
JSONObject myObj = response.getBody().getObject();
// extract fields from the object
String msg = myObj.toString();

System.out.println(msg);

但我在获取原始 JSON 文本时遇到问题(我想使用 JSONPath 来解析响应)。

我该怎么做?到目前为止,我所有调用 toString() 方法的尝试都失败了。

最佳答案

Unirest API 支持开箱即用 - 使用 asString()而不是 asJson()得到响应 HttpResponse<String> .

HttpResponse<String> response = Unirest
.post("http://myserver.com/file")
.header("cache-control", "no-cache")
.header("Postman-Token", "02ec2fa1-afdf-4a2a-a535-353424d99400")
.header("Content-Type", "application/json")
.body("{some JSON body}")
.asString();
System.out.println(response.getBody());

关于java - 如何从 Java 中的 Unirest 响应中获取原始 JSON 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53751574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com