programing

UTF-8 문자를 사용하여 오브젝트노드를 JSON 문자열로 이스케이프 ASC에 쓰기II

sourcejob 2023. 3. 10. 21:27
반응형

UTF-8 문자를 사용하여 오브젝트노드를 JSON 문자열로 이스케이프 ASC에 쓰기II

나는 잭슨의 내용을 쓰고 싶다.ObjectNodeUTF-8 문자가 ASCII(Unicode excape)로 기술된 문자열로 이동합니다.

다음은 방법의 예를 제시하겠습니다.

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

기본적으로는 다음과 같은 출력이 있습니다.

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

출력할 내용은 다음과 같습니다.

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

어떻게 하면 좋을까요?

비 ASC의 이스케이프를 제어하는 JsonGenerator 기능을 활성화해야 합니다.II 문자다음은 예를 제시하겠습니다.

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

출력은 다음과 같습니다.

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

Json Generator 대신 Json Write Feature를 사용하는 것은 권장되지 않습니다.

 mapper.getFactory().configure(JsonWriteFeature.ESCAPE_NON_ASCII.mappedFeature(), true);

언급URL : https://stackoverflow.com/questions/23121765/write-objectnode-to-json-string-with-utf-8-characters-to-escaped-ascii

반응형