使用 Jackson 将 JSON 字符串转换为 Pretty Print JSON 输出

2022-08-31 06:23:27

这是我拥有的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转换为Pretty Print JSON Output(使用Jackson),如下所示:String

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

谁能告诉我,我是否得到了正确的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));

然后在结果表单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" } ] }

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


答案 1

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

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

然后用缩进写出来:

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

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

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


答案 2

最简单也是最紧凑的解决方案(适用于 v2.3.3):

ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
mapper.writeValueAsString(obj)