我想将Jackson's ObjectNode的内容写入一个字符串,其中UTF-8字符被写成ASCII(Unicode转义).

Here is a sample method:

private String writeUnicodeString() {
    ObjectMapper mapper = new ObjectMapper();
    ObjectNode node = mapper.getNodeFactory().objectNode();
    node.put("field1", "Maël Hörz");
    return node.toString();
}

By default, this outputs:

{"field1":"Maël Hörz"}

What I would like it to output is:

{"field1":"Ma\u00EBl H\u00F6rz"}

How can I accomplish this?

推荐答案

您应该启用JsonGenerator功能,该功能控制非ASCII字符的转义.下面是一个例子:

    ObjectMapper mapper = new ObjectMapper();
    mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
    ObjectNode node = mapper.getNodeFactory().objectNode();
    node.put("field1", "Maël Hörz");
    System.out.println(mapper.writeValueAsString(node));

The output is:

{"field1":"Ma\u00EBl H\u00F6rz"}

Json相关问答推荐

jq不会为空输入返回非零

手动解开没有可编码的SON- Swift

Golang JSON Date Tim.Date()测试请求

震击:三针并用震击

Pandas 对REST API的自定义响应

如何从一个700MB的json文件中列出PowerShell中的所有密钥?

合并2个嵌套词典

在Ruby的json中压缩单个字段

VBA-JSON 嵌套集合解析为 Excel

使用 jq 将消息转换为数组

Powershell 7.2:ConvertFrom-Json - 日期处理

Scala - 在构建 Json 时无法删除 Key -> value "{}" 大括号的双引号

PowerShell:如何将哈希表输出为 json 并使用 foreach 循环将其存储在变量中?

在 bash 中将 CSV 转换为 JSON

Json.Net:用于自定义命名的 JsonSerializer-Attribute

jQuery.getJSON 和 jQuery.parseJSON 返回 [object Object]?

Spring MVC:不反序列化 JSON 请求正文

将序列化表单的数据转换为 json 对象

通过 JSON 发送 64 位值的公认方式是什么?

使用 JSONArray 和 JSONObject 进行 Foreach