Unexpected character ('=' (code 61)): while parsing JsonString

Joy picture Joy · Jul 17, 2018 · Viewed 7.1k times · Source

While parsing a JSONString (assigned=[util.TaskAudit@24c7b944]}) as follows:

Map<String, List<TaskAudit>> auditTrailMap = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

try {
    auditTrailMap = mapper.readValue(strObject, new TypeReference<Map<String, List<TaskAudit>>>(){});
} catch (IOException e) {
    log.error("{}", e);
}

I am getting following exception:

com.fasterxml.jackson.core.JsonParseException: Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value [junit] at [Source: {assigned=[util.TaskAudit@24c7b944]}; line: 1, column: 11]

Can anyone please give any clue regarding how to fix that.

Edit: Basically the input of this function is a string field coming from database. While saving we save it like this:

        Map<String, List<TaskAudit>> auditTrailMap = new HashMap<>();
        auditTrailMap.put("assigned", taskAuditList);
        String jsonString =  new JSONObject(auditTrailMap).toString();

But while trying to parse the jsonString it comes like (assigned=[util.TaskAudit@24c7b944]}), I am not getting any idea where '=' is coming from and how to parse it.

Answer

Shashi Bhushan picture Shashi Bhushan · Jul 17, 2018

Problem with your approach is "You are trying to parse a MAP into a JSON in a wrong way."

You need to use ObjectMapper there to parse your MAP into a JSON string. Once you do that, you should be able to get the right JSON string as you expected based on KEYs and VALUEs present in the Map.

   Map<String, List<TaskAudit>> auditTrailMap = new HashMap<>();
   auditTrailMap.put("assigned", taskAuditList);
   String jsonString =  new ObjectMapper().writeValueAsString(auditTrailMap); 

Once you do this, you will get proper JSON: { "assigned" : "corresponding value for the key which you set in the map" }

Hope this helps your intended requirement.