在JAVA中为JSON进行漂亮的打印在控制台上工作正常,但在浏览器中却无法正常工作 [英] Pretty print for JSON in JAVA works fine for the console, but in browser it does not work

查看:111
本文介绍了在JAVA中为JSON进行漂亮的打印在控制台上工作正常,但在浏览器中却无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件,我想从Java Spring Boot中创建的rest控制器中的API调用中检索其内容.我将.json文件的内容转换为字符串,并使用以下方法(其中一种)进行漂亮的打印.如果我将system.out.println()输出,它会被漂亮地打印出来,但是在浏览器中它会粗略显示并且没有缩进.我有更多方法:

I have a JSON file and I want to retrieve its content from a API call within a rest controller created in Java Spring Boot. I get the content of the .json file into a String and use the below method ( one of them ) in order to pretty print. If I system.out.println() the output, it gets pretty printed, but in the browser it is displayed roughly and with no indentation. I had more approaches :

    String content = new String(Files.readAllBytes(resource.toPath()));

    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    JsonParser jp = new JsonParser();
    JsonElement je = jp.parse(content);
    String prettyJsonString = gson.toJson(je);
    System.out.println(prettyJsonString);

    return prettyJsonString;

另一种方法在浏览器中返回相同的丑陋输出,但是它也添加了"/r/n":

The other approach returns the same ugly output in browser, but it also adds "/r/n" :

    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    String prettyJsonString = mapper.writeValueAsString(content);

    return prettyJsonString;

任何人都可以帮助我在浏览器中获得漂亮的输出吗?

Can anyone help me get the pretty output in browser as well ?

推荐答案

为控制台输出和HTML输出格式化字符串是两个非常不同的任务.方法 setPrettyPrinting()用于控制台打印.HTML浏览器将忽略"\ n"符号,并且不会使用多个空格替换单个空格等.通常,格式化输出通常是一项客户端任务.但是我曾就此问题进行过一次探讨,并编写了一个方法,该方法采用控制台格式的字符串并将其转换为HTML格式的String.例如,它用 br HTML标签替换所有"\ n"符号.它也做其他一些事情.我在其中取得了一些成功,但有时会发生一些意外的问题.欢迎您使用它.该方法在MgntUtils开源库中可用.这是它的 JavaDoc .该库本身可以作为Maven工件在此处并在Github上(包括源代码和JavaDoc)在此处.关于该库的文章是

Formatting String for console output and for HTML output are two VERY different tasks. Method setPrettyPrinting() is for console printing. HTML browser will ignore "\n" symbols and will not respect multiple spaces replacing them with a single space etc. In general, it is usually a client-side task to format the output. But I delt once with this problem and wrote a method that takes a console-formatted string and converts it to Html formatted String. For instance, it replaces all "\n" symbols with br Html tags. It does some other things as well. I had some success with it, but sometimes some unexpected problems occurred. You are welcome to use it. The method is available in MgntUtils Open source library. Here is its JavaDoc. The library itself is available as Maven artifact here and on Github (including source code and JavaDoc) here. An article about the library is here. Your code would look like this:

String htmlString = TextUtils.formatStringToPreserveIndentationForHtml(jsonPrettyString);

这篇关于在JAVA中为JSON进行漂亮的打印在控制台上工作正常,但在浏览器中却无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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