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

查看:472
本文介绍了使用Jackson将JSON字符串转换为Pretty Print 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字符串转换为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 basis on my example above? How to achieve this scenario, I know there are lot of example but I am not able to understand those properly. Any help will be appreciated with a simple example.

更新:

以下是我正在使用的代码

Below is the code I am using

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\n java.util.concurrent.CancellationException\n\tat java.util.concurrent.FutureTask$Sync.innerGet(FutureTask.java:231)\n\tat 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));

然后在结果表jsp页面中显示出来,如下所示 -

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

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

    </fieldset>

我得到这样的东西 -

I get something like this-

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

我不需要。我需要它打印出来的方式。谁能告诉我为什么会这样发生?

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

推荐答案

要缩进任何旧的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字符串转换为Pretty Print JSON输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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