jozinho22
jozinho22

Reputation: 592

How to get rid of ' characters in JSON Serialization?

I want to serialize an object using Jackson. Here's the json :

[
    {
        "texte": "Quel sont les trois grands principes de la POO ?",
        "topic": "Java",
        "reponses": [
            {
                "texte": "L\\'encapsulation, l\\'héritage et le polymorphisme.",
                "isTrue": true
            },
            {
                "texte": "L\\'encapsulation, l\\'héritage multiple et le polymorphisme.",
                "isTrue": false
            },
            {
                "texte": "Le multi-threading, l\\'accès aux données et le polymorphisme.",
                "isTrue": false
            }
        ]
    },
    {
        "texte": "Quel interface implémente la classe ArrayList ?",
        "topic": "Java",
        "reponses": [
            {
                "texte": "List",
                "isTrue": true
            },
            {
                "texte": "Queue",
                "isTrue": false
            },
            {
                "texte": "Serializable",
                "isTrue": false
            }
        ]
    }
]

Here's the Java code :

public static void main(String[] args) throws IOException {
        FileReader reader = new FileReader();
        File jsonFile = reader.getFile("questions/datas.json");

        ObjectMapper mapper = new ObjectMapper();
        
        List<Question> questions = mapper.reader()
                  .forType(new TypeReference<List<Question>>() {})
                  .readValue(jsonFile);
        
        System.out.println(questions);

    }

Finally I have this error :

Invalid UTF-8 middle byte 0x72 at [Source:

C:\Users\josselin.douineau\projects\quizz-java-generator\target\classes\questions\datas.json; line: 7, column: 41] (through reference chain: java.util.ArrayList[0]->com.douineau.entity.Question["reponses"]->java.util.ArrayList[0]-com.douineau.entity.Reponse["texte"])

Does anybody know what is it about ? I'm not coming from the IT, so I'm not very good to understand this type of message.

Upvotes: 0

Views: 376

Answers (1)

lucid
lucid

Reputation: 2900

Error is caused by escape character \\' in l\\'héritage et. you can configure object mapper to allow escape characters.

ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER, true);

Upvotes: 3

Related Questions