ティナJ:
私は、サーバーへの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()
の方法はこれまでに失敗しました。
Neeraj:
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());