這是JSON字符串我有─轉換JSON字符串到漂亮的打印JSON輸出使用傑克遜
{"attributes":[{"nm":"ACCOUNT","lv":[{"v":{"Id":null,"State":null},"vt":"java.util.Map","cn":1}],"vt":"java.util.Map","status":"SUCCESS","lmd":13585},{"nm":"PROFILE","lv":[{"v":{"Party":null,"Ads":null},"vt":"java.util.Map","cn":2}],"vt":"java.util.Map","status":"SUCCESS","lmd":41962}]}
我需要上述JSON字符串轉換成漂亮的打印JSON輸出(使用傑克遜)等如下─
{
"attributes": [
{
"nm": "ACCOUNT",
"lv": [
{
"v": {
"Id": null,
"State": null
},
"vt": "java.util.Map",
"cn": 1
}
],
"vt": "java.util.Map",
"status": "SUCCESS",
"lmd": 13585
},
{
"nm": "PROFILE
"lv": [
{
"v": {
"Party": null,
"Ads": null
},
"vt": "java.util.Map",
"cn": 2
}
],
"vt": "java.util.Map",
"status": "SUCCESS",
"lmd": 41962
}
]
}
任何人都可以在我上面的示例中提供示例基礎嗎?如何實現這種情況,我知道有很多例子,但我無法正確理解這些。任何幫助將通過一個簡單的例子來體會。
更新時間:
下面是我使用
ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(jsonString));
代碼但是,這並不與我所需要的輸出上面
這裏所提到的工作方式是POJO我用於上述JSON-
public class UrlInfo implements Serializable {
private List<Attributes> attribute;
}
class Attributes {
private String nm;
private List<ValueList> lv;
private String vt;
private String status;
private String lmd;
}
class ValueList {
private String vt;
private String cn;
private List<String> v;
}
任何人都可以告訴我是否爲JSON獲得了正確的POJO嗎?
更新: -
String result = restTemplate.getForObject(url.toString(), String.class);
ObjectMapper mapper = new ObjectMapper();
Object json = mapper.readValue(result, Object.class);
String indented = mapper.defaultPrettyPrintingWriter().writeValueAsString(json);
System.out.println(indented);//This print statement show correct way I need
model.addAttribute("response", (indented));
以下行打印出來的東西像這 -
System.out.println(indented);
{
"attributes" : [ {
"nm" : "ACCOUNT",
"error" : "null SYS00019CancellationException in CoreImpl fetchAttributes\n java.util.concurrent.CancellationException\n\tat java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat java.util.concurrent.FutureTask.",
"status" : "ERROR"
} ]
}
這是我需要的顯示方式。但是,當我把它添加到模型像這個 -
model.addAttribute("response", (indented));
,然後顯示出來的resultform jsp頁面類似如下─
<fieldset>
<legend>Response:</legend>
<strong>${response}</strong><br />
</fieldset>
我得到的東西像這 -
{ "attributes" : [ { "nm" : "ACCOUNT", "error" : "null
SYS00019CancellationException in CoreImpl fetchAttributes\n
java.util.concurrent.CancellationException\n\tat
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat
java.util.concurrent.FutureTask.", "status" : "ERROR" } ] }
我不需要。我需要它打印出來的方式。誰能告訴我爲什麼會發生這樣的事情?
如果您向我們展示您迄今爲止編寫的代碼,那將會很棒。 – Swapnil
@Swapnil,用我到目前爲止的代碼更新了這個問題。 – ferhan