gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:37:55 26 4
gpt4 key购买 nike

我有这个代码:

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

我从服务器端创建一个静态 html 页面(使用 gagawa):

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

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

但我的 html 看起来很糟糕。

您如何将 map 美化为 html?

*有没有办法通过#chars 来限制行内容?*一种保持漂亮打印品的方法?

enter image description here

最佳答案

最简单的方法是在您的页面中添加一些 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>

如您所见,第二个 liwrap类在框架边缘换行,而另一个则没有。


如果您需要 json 打印得漂亮,您需要修改输出 HTML 并将 json 包装在 <pre> 中标签。默认情况下,这些将保留换行符和空格。

关于javascript - 如何将漂亮的json格式嵌入到html静态页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35303720/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com