如何让杰克逊使用单引号或无引号生成json字符串? [英] How to let jackson generate json string using single quote or no quotes?

查看:526
本文介绍了如何让杰克逊使用单引号或无引号生成json字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想为ng-style生成一个json字符串:

For example, I want to generate a json string for ng-style:

<th ng-style="{width:247}" data-field="code">Code</th>

但是在杰克逊的情况下,结果是:

But with jackson, the result is:

<th ng-style="{&quot;width&quot;:247}" data-field="code">Code</th>

这不容易阅读.

所以我想 jackson 生成带有单引号或不带引号的json字符串.可以这样做吗?

So I want jackson to generate the json string with single quote or no quotes. Is it possible to do this?

推荐答案

如果您可以控制ObjectMapper实例,则可以将其配置为以所需方式处理和生成JSON:

If you have control over the ObjectMapper instance, then configure it to handle and generate JSON the way you want:

final ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonGenerator.Feature.QUOTE_FIELD_NAMES, false);
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

这篇关于如何让杰克逊使用单引号或无引号生成json字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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