简体   繁体   English

如何将漂亮的json格式嵌入html静态页面?

[英]how to embed pretty json format to html static page?

I have this code: 我有以下代码:

 public <T> String toJson(T item)
    {
        String json;
        ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
        try {
            json = ow.writeValueAsString(item);
        } catch (IOException e) {
            e.printStackTrace();
            json = "";
        }
        return json;
    }

I create a static html page form the server side (using gagawa ): 我在服务器端创建了一个静态html页面(使用gagawa ):

    java.util.Map<String, String> paramsMap = filterInterestingQueryParams(url);

    Li li4 = new Li().appendText("special params: " + jsonParser.toJson(paramsMap));

and yet my html looks broken. 但是我的html看起来坏了。

How would you prettify that map into html? 您如何将地图美化为html?

*is there a way to limit line content by #chars? *是否可以通过#chars限制行内容? *a way to persist the pretty print? *一种保持漂亮印刷品的方式?

在此处输入图片说明

The simplest way is to add some CSS to your page: 最简单的方法是在页面中添加一些CSS:

 li.wrap { word-wrap:break-word; } 
 <ul> <li>Really long special params: {stickToPath=8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,}</li> <li class="wrap">Really long special params (that wraps): {stickToPath=8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,8484.20202002,-481737.45454,}</li> </ul> 

As you can see, the second li with the wrap class wraps at the frame edge, while the other doesn't. 如您所见,具有wrap类的第二个li包装在框架边缘,而另一个则没有。


If you need the json to be pretty print you'll need to modify your output HTML and wrap the json in <pre> tags. 如果您需要漂亮地打印json,则需要修改输出HTML并将json包装在<pre>标记中。 By default these will retain line breaks and spaces. 默认情况下,这些将保留换行符和空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM