ars*_*nal 146 java json pretty-print jackson
这是我的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转换为Pretty Print JSON Output(使用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;
}
谁能告诉我是否为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" } ] }
我不需要.我需要从上面打印出来的方式.谁能告诉我为什么会这样?
Sta*_*Man 226
要缩进任何旧的JSON,只需将其绑定为Object,如:
Object json = mapper.readValue(input, Object.class);
然后用缩进写出来:
String indented = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
这可以避免您必须定义实际的POJO来映射数据.
或者您也可以使用JsonNode(JSON树).
小智 118
最简单也是最紧凑的解决方案(适用于v2.3.3):
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
Mar*_*les 24
使用Jackson 1.9+的新方法如下:
Object json = OBJECT_MAPPER.readValue(diffResponseJson, Object.class);
String indented = OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
                               .writeValueAsString(json);
输出将正确格式化!
sca*_*ser 15
对于Jackson 1.9,我们可以使用以下代码进行漂亮的打印.
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.enable(SerializationConfig.Feature.INDENT_OUTPUT);
Pra*_*eth 10
ObjectMapper.readTree() 可以在一行中做到这一点:
mapper.readTree(json).toPrettyString();
由于readTree产生 a JsonNode,这几乎总是产生等效的漂亮格式的 JSON,因为它JsonNode是底层 JSON 字符串的直接树表示。
该JsonNode.toPrettyString()方法是在 Jackson 2.10 中添加的。在此之前,需要第二次调用ObjectMapper来写出漂亮的格式化结果:
mapper.writerWithDefaultPrettyPrinter()
        .writeValueAsString(mapper.readTree(json));
我认为,这是美化json数据最简单的技术,
String indented = (new JSONObject(Response)).toString(4);
其中Response是一个字符串.
只需在toString()方法中传递4(indentSpaces).
注意:它在没有任何库的android中工作正常.但是在java中你必须使用org.json库.
您可以使用以下方式实现此目的:
1.利用杰克逊
    String formattedData=new ObjectMapper().writerWithDefaultPrettyPrinter()
.writeValueAsString(YOUR_JSON_OBJECT);
导入波纹管类:
import com.fasterxml.jackson.databind.ObjectMapper;
它的 gradle 依赖项是:
compile 'com.fasterxml.jackson.core:jackson-core:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.7.3'
compile 'com.fasterxml.jackson.core:jackson-databind:2.7.3'
2.使用谷歌的Gson
String formattedData=new GsonBuilder().setPrettyPrinting()
    .create().toJson(YOUR_OBJECT);
导入波纹管类:
import com.google.gson.Gson;
它的等级是:
compile 'com.google.code.gson:gson:2.8.2'
在这里,您还可以从存储库下载正确的更新版本。
| 归档时间: | 
 | 
| 查看次数: | 196011 次 | 
| 最近记录: |