这是我拥有的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 String
转换成漂亮的Print JSON输出(使用Jackson),如下所示:
{
"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));
但这与我需要上面提到的输出的方式不同。
下面是我在上面的JSON中使用的POJO:
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;
}
谁能告诉我,我是否得到了正确的POJO的JSON?
更新:
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));
然后在如下所示的结果表单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" } ] }
但我不需要。我需要上面打印出来的方式。谁能告诉我为什么会这样?
发布于 2013-01-26 08:25:45
要缩进任何旧的JSON,只需将其绑定为Object
,如下所示:
Object json = mapper.readValue(input, Object.class);
然后使用缩进将其写出来:
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
这避免了您必须定义要将数据映射到的实际POJO。
或者你也可以使用JsonNode
(JSON Tree)。
发布于 2014-06-02 01:32:09
最简单也是最紧凑的解决方案(对于v2.3.3):
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValueAsString(obj)
发布于 2014-01-14 10:49:07
使用Jackson 1.9+的新方法如下:
Object json = OBJECT_MAPPER.readValue(diffResponseJson, Object.class);
String indented = OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(json);
输出将被正确格式化!
https://stackoverflow.com/questions/14515994
复制相似问题