通过Play WS API,我get()一个Response对象。因为它包含JSON,所以我称之为
response.asJson()
效果很好。现在,我想以 pretty-print 的版本返回此JSON,所以我尝试调用
Json.prettyPrint(response.asJson())
但是,这不起作用,因为prettyPrint需要JsValue,而不是JsonNode。
那么问题是如何将JsonNode转换为JsObject?
最佳答案
我猜您正在使用Play with Java。除了转换为JsValue之外,您还可以执行以下操作:
JsonNode node = response.asJson();
ObjectMapper mapper = new ObjectMapper();
String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);