Java Json漂亮打印javax.Json

Java Json漂亮打印javax.Json,java,json,Java,Json,我正试图用javax.json API漂亮地打印json 我目前使用的代码如下: private String prettyPrint(String json) { StringWriter sw = new StringWriter(); try { JsonReader jr = Json.createReader(new StringReader(json)); JsonObject jobj = jr.r

我正试图用javax.json API漂亮地打印json

我目前使用的代码如下:

private String prettyPrint(String json) {
        StringWriter sw = new StringWriter();

        try {
            JsonReader jr = Json.createReader(new StringReader(json));

            JsonObject jobj = jr.readObject();



            Map<String, Object> properties = new HashMap<>(1);
            properties.put(JsonGenerator.PRETTY_PRINTING, true);


            JsonGeneratorFactory jf = Json.createGeneratorFactory(properties);
            JsonGenerator jg = jf.createGenerator(sw);

            jg.write(jobj).close();


        } catch (Exception e) {
        }

        String prettyPrinted = sw.toString();

        return prettyPrinted;
    }

您应该使用JsonWriter而不是JsonGenerator

更换这些线路:

JsonGeneratorFactory jf = Json.createGeneratorFactory(properties);
JsonGenerator jg = jf.createGenerator(sw);

jg.write(jobj).close();
有了这些:

JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
JsonWriter jsonWriter = writerFactory.createWriter(sw);

jsonWriter.writeObject(jobj);
jsonWriter.close();

下面是一个使用Jersey使用
javax.JSON
javax.ws.rs
漂亮打印(缩进)JSON的解决方案:

@GET
@Path("stuff")
public Response getStuff(@QueryParam("pretty") boolean pretty) {
    JsonArrayBuilder stuff = Json.createArrayBuilder().add("foo").add("bar");
    JsonObject jsonObject = Json.createObjectBuilder()
            .add("status", "OK")
            .add("data", stuff).build();
    if (pretty) {
        Map<String, Boolean> config = new HashMap<>();
        config.put(JsonGenerator.PRETTY_PRINTING, true);
        JsonWriterFactory jwf = Json.createWriterFactory(config);
        StringWriter sw = new StringWriter();
        try (JsonWriter jsonWriter = jwf.createWriter(sw)) {
            jsonWriter.writeObject(jsonObject);
        }
        // return "Content-Type: application/json", not "text/plain"
        MediaType mediaType = MediaType.APPLICATION_JSON_TYPE;
        return Response.ok(sw.toString(), mediaType).build();
    } else {
        return Response.ok(jsonObject).build();
    }

}
另见:

  • JSR 353:JSON处理的Java API:
  • 用于JSON处理的Java API(JSON-P):
  • 泽西岛:

您可以通过使用
JsonWriterFactory
实现漂亮的打印效果。这需要属性的配置映射

使用
JsonWriter
写入
StringWriter

我添加了一个方便的方法,它已经为您传递了
PRETTY\u PRINTING
标志

public static String prettyPrint(JsonStructure json) {
    return jsonFormat(json, JsonGenerator.PRETTY_PRINTING);
}

public static String jsonFormat(JsonStructure json, String... options) {
    StringWriter stringWriter = new StringWriter();
    Map<String, Boolean> config = buildConfig(options);
    JsonWriterFactory writerFactory = Json.createWriterFactory(config);
    JsonWriter jsonWriter = writerFactory.createWriter(stringWriter);

    jsonWriter.write(json);
    jsonWriter.close();

    return stringWriter.toString();
}

private static Map<String, Boolean> buildConfig(String... options) {
    Map<String, Boolean> config = new HashMap<String, Boolean>();

    if (options != null) {
        for (String option : options) {
            config.put(option, true);
        }
    }

    return config;
}
公共静态字符串预打印(JsonStructure json){
返回jsonFormat(json,JsonGenerator.PRETTY_PRINTING);
}
公共静态字符串jsonFormat(JsonStructure json、字符串…选项){
StringWriter StringWriter=新StringWriter();
Map config=buildConfig(选项);
JsonWriterFactory writerFactory=Json.createWriterFactory(config);
JsonWriter=writerFactory.createWriter(stringWriter);
jsonWriter.write(json);
jsonWriter.close();
返回stringWriter.toString();
}
私有静态映射buildConfig(字符串…选项){
Map config=newhashmap();
如果(选项!=null){
用于(字符串选项:选项){
config.put(选项,true);
}
}
返回配置;
}

永远不要清空捕获物。永远不会。原谅我的懒惰,你是对的。我已经更新了帖子。似乎jg.write(jobj.close();这是一个可以用来快速检查JSON的问题。
$ curl -i http://localhost:8080/api/stuff?pretty=true
HTTP/1.1 200 OK
Content-Type: application/json
Date: Fri, 08 Aug 2014 14:32:40 GMT
Content-Length: 71


{
    "status":"OK",
    "data":[
        "foo",
        "bar"
    ]
} 
$ curl http://localhost:8080/api/stuff
{"status":"OK","data":["foo","bar"]}
public static String prettyPrint(JsonStructure json) {
    return jsonFormat(json, JsonGenerator.PRETTY_PRINTING);
}

public static String jsonFormat(JsonStructure json, String... options) {
    StringWriter stringWriter = new StringWriter();
    Map<String, Boolean> config = buildConfig(options);
    JsonWriterFactory writerFactory = Json.createWriterFactory(config);
    JsonWriter jsonWriter = writerFactory.createWriter(stringWriter);

    jsonWriter.write(json);
    jsonWriter.close();

    return stringWriter.toString();
}

private static Map<String, Boolean> buildConfig(String... options) {
    Map<String, Boolean> config = new HashMap<String, Boolean>();

    if (options != null) {
        for (String option : options) {
            config.put(option, true);
        }
    }

    return config;
}