jackson2 设置特殊字符不加反斜杆
时间: 2023-10-13 11:30:48 浏览: 130
您可以在 Jackson 序列化器中通过设置 JsonGenerator 的 escapeCharacters 方法来设置特殊字符不加反斜杆。具体代码如下:
```java
ObjectMapper mapper = new ObjectMapper();
CharacterEscapes customEscapes = new CharacterEscapes() {
private final int[] asciiEscapes;
{
asciiEscapes = CharacterEscapes.standardAsciiEscapesForJSON();
asciiEscapes['/'] = CharacterEscapes.ESCAPE_NONE;
}
@Override
public int[] getEscapeCodesForAscii() {
return asciiEscapes;
}
@Override
public SerializableString getEscapeSequence(int ch) {
// not used for standard ASCII characters
return null;
}
};
mapper.getFactory().setCharacterEscapes(customEscapes);
```
在上面的代码中,我们将斜杠字符 `/` 的转义码设置为 `ESCAPE_NONE`,这样就可以避免它被转义了。您可以在 `asciiEscapes` 数组中设置其他需要特殊处理的字符的转义码。
阅读全文