使用 Jackson 将 JSON 字符串转换为漂亮的打印 JSON 输出 [英] Convert JSON String to Pretty Print JSON output using Jackson

查看:63
本文介绍了使用 Jackson 将 JSON 字符串转换为漂亮的打印 JSON 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我拥有的 JSON 字符串:

This is the JSON string I have:

{"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 转换为 Pretty Print JSON 输出(使用 Jackson),如下所示:

I need to convert the above JSON String into Pretty Print JSON Output (using Jackson), like below:

{
    "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
        }
    ]
}

任何人都可以根据我上面的例子提供一个例子吗?如何实现这个场景?我知道有很多例子,但我无法正确理解这些.任何帮助将通过一个简单的例子表示赞赏.

Can anyone provide me an example based on my example above? How to achieve this scenario? I know there are lot of examples, but I am not able to understand those properly. Any help will be appreciated with a simple example.

更新:

以下是我使用的代码:

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.defaultPrettyPrintingWriter().writeValueAsString(jsonString));

但这不适用于我上面提到的需要输出的方式.

But this doesn't works with the way I needed the output as mentioned above.

这是我用于上述 JSON 的 POJO:

Here's is the POJO I am using for the above 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?

Can anyone tell me whether I got the right POJO for the JSON or not?

更新:

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));

下面一行打印出如下内容:

Below line prints out something like this:

System.out.println(indented);


{
  "attributes" : [ {
    "nm" : "ACCOUNT",
    "error" : "null SYS00019CancellationException in CoreImpl fetchAttributes
 java.util.concurrent.CancellationException
	at java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)
	at java.util.concurrent.FutureTask.",
    "status" : "ERROR"
  } ]
}

这是我需要展示的方式.但是当我像这样将它添加到模型中时:

which is the way I needed to be shown. But when I add it to model like this:

model.addAttribute("response", (indented));

然后在一个resultform jsp页面中显示出来,如下所示:

And then shows it out in a resultform jsp page like below:

    <fieldset>
        <legend>Response:</legend>
            <strong>${response}</strong><br />

    </fieldset>

我得到了这样的东西:

{ "attributes" : [ { "nm" : "ACCOUNT", "error" : "null    
SYS00019CancellationException in CoreImpl fetchAttributes
 
java.util.concurrent.CancellationException
	at 
java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)
	at 
java.util.concurrent.FutureTask.", "status" : "ERROR" } ] }

我不需要.我需要上面打印出来的方式.谁能告诉我为什么会这样?

which I don't need. I needed the way it got printed out above. Can anyone tell me why it happened this way?

推荐答案

要缩进任何旧的 JSON,只需将其绑定为 Object,例如:

To indent any old JSON, just bind it as Object, like:

Object json = mapper.readValue(input, Object.class);

然后用缩进写出来:

String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);

这避免了您必须定义实际的 POJO 以将数据映射到.

this avoids your having to define actual POJO to map data to.

或者您也可以使用 JsonNode(JSON 树).

Or you can use JsonNode (JSON Tree) as well.

这篇关于使用 Jackson 将 JSON 字符串转换为漂亮的打印 JSON 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆