1

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.

Joy
  • 4,197
  • 14
  • 61
  • 131
  • 4
    `(assigned=[util.TaskAudit@24c7b944]})` is not a valid JSON string – lucasvw Jul 17 '18 at 17:57
  • Hi @lucasvw this string is generated while to a toString() on a map from another API. The generated string is the input of the above function. – Joy Jul 17 '18 at 18:00
  • how to fix? Fix JSON – Jacek Cz Jul 17 '18 at 18:07
  • The `=` is not used in JSON, which describes _data_, not functions. I suggest you do a little basic reading about JSON. It generally represents arrays and objects, where objects use `"key" : "value"` pairs. [Here's an example](https://www.json.org/example.html) from json.org – Stephen P Jul 17 '18 at 18:14
  • What is that `JSONObject` class? Almost certainly, its `toString()` method isn't doing what you think it does. – Daniel Pryden Jul 17 '18 at 18:17

3 Answers3

5

Problem with your approach is that you are trying to parse a Map into a JSON in a wrong way.

You need to use Jackson's 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.

4

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

You already have that

{assigned=[util.TaskAudit@24c7b944]}

is not a valid JSON string.

toString() method of Map is not required to return you JSON representation, so you have to serialize it to JSON yourself, like in this answer:

Map<String,String> payload = new HashMap<>();
payload.put("key1","value1");
payload.put("key2","value2");

// right way to convert map to JSON:
String json = new ObjectMapper().writeValueAsString(payload); 

Adapt the code above to your needs, probably reusing ObjectMapper instance.

And read the docs and examples first before adapting new technology, it will save you a lot of time in the future, because they usually highlight most common cases people use library for.

Pavlus
  • 1,651
  • 1
  • 13
  • 24
1
ObjectMapper om=new ObjectMapper();
    om.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);

    List<RootMember> outputList;
    try {
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(resp); 
        outputList = om.readValue(json, new TypeReference<List<RootMember>>(){});
    } catch (JsonMappingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }