Search code examples
javajsonparsinghashmaptreemap

how to parse given data into a java object


I want to parse below given data in to some java object, but I am not able to parse. String is as follows -

{\"objectsTree\":\"{\"Address\":[],\"Customer\":[\"Address\"]}\",\"objectsSequence\":\"[\"Customer\",\"Address\"]\"}

I have tried parsing this into HashMap and HashMap but this is returning malformed JSON exception, and that is making sense, because of too many double quotes objects are ending abruptly. I want to parse this as follows-

{
"objectsTree":"{"Address":[],"Customer":["Address"]}",
"objectsSequence":"["Customer","Address"]"
}

here you can see that objectsTree is one object against one string and objectSequence is another. to be specific object tree is supposed to be a treemap , object sequence is supposed to be a ArrayList.

Any Idea how should I proceed.

code update-

package org.syncoms.backofficesuite.controller;

import java.util.HashMap;

import com.google.gson.Gson;

public class Test {

public static void main(String[] args) {

    //String json = "{\"Success\":true,\"Message\":\"Invalid access token.\"}";
    String json ="{\"objectsTree\":\"{\"Address\":[],\"Customer\":[\"Address\"]}\",\"objectsSequence\":\"[\"Customer\",\"Address\"]\"}";
    Gson jsonParser = new Gson();
    @SuppressWarnings("unchecked")
    HashMap<String,Object> jo = (HashMap<String,Object>) jsonParser.fromJson(json, HashMap.class);
    System.out.println(jo);
    //Assert.assertNotNull(jo);
    //Assert.assertTrue(jo.get("Success").getAsString());


}

}

the error which I am getting -

Exception in thread "main" com.google.gson.JsonParseException: Failed parsing JSON source: java.io.StringReader@201644c9 to Json
    at com.google.gson.JsonParser.parse(JsonParser.java:59)
    at com.google.gson.Gson.fromJson(Gson.java:443)
    at com.google.gson.Gson.fromJson(Gson.java:396)
    at com.google.gson.Gson.fromJson(Gson.java:372)
    at org.syncoms.backofficesuite.controller.Test.main(Test.java:16)
Caused by: com.google.gson.ParseException: Encountered " <IDENTIFIER_SANS_EXPONENT> "Address "" at line 1, column 19.
Was expecting one of:
    "}" ...
    "," ...

Solution

  • The main issue here is that the input is simply not a valid JSON String, and no JSON parser is going to accept it. the doulbe qoutes have to be escaped. a Valid JSON String is as follows:

    String jsonInput = "{\"objectsTree\":\"{\\\"Address\\\":[],\\\"Customer\\\":[\\\"Address\\\"]}\",\"objectsSequence\":\"[\\\"Customer\\\",\\\"Address\\\"]\"}";
    

    and this can be parsed using, for instance, Jackson:

    ObjectMapper om = new ObjectMapper();
    TypeFactory tf = om.getTypeFactory();
    JavaType mapType = tf.constructMapType(HashMap.class, String.class, String.class);
    Map<String, String> map = (Map<String, String>)om.readValue(jsonInput, mapType);
    System.out.println(map);
    

    Printout is:

    {objectsSequence=["Customer","Address"], objectsTree={"Address":[],"Customer":["Address"]}}