How to convert the map of JSON objects to JSON using GSON in Java?

advertisements

I have a map of JSON objects as follows:

Map<String,Object> map = HashMap<String,Object>();
map.put("first_name", "prod");
JSONObject jsonObj = new JSONObject("some complex json string here");
map.put("data", jsonObj);

Gson gson = new Gson();
String result = gson.toJson(map);

Now if the "some complex JSON string here" was:

{"sender":{"id":"test test"},"recipients":{"id":"test1 test1"} }

and execute above code gives me something like:

{
    "first_name": "prod",
    "data": {
        "map": {
            "sender": {
                "map": {
                    "id": "test test"
                    }
                }
            },
            "recipients": {
                "map": {
                    "id": "test1 test1"
                }
            }
        }
    }
}

I might have some syntax error up there, but basically I don't know why I am seeing objects wrapped around map's.

Update

according to comments, it is a bad idea to mix different json parsers. i can understand that. but my case requires calling an external api which takes a hash map of objects that are deserialized using gson eventually.

is there any other object bedsides JSONObject that i can add to the map and still have gson create json out of it without extra 'map' structure? i do understand that i can create java beans and achieve this. but i'm looking for a simpler way since my data structure can be complex.

Update2

going one step back, i am given a xml string. and i have converted them to json object. now i have to use an external api that takes a map which in turn gets converted to json string using gson in external service.

so i am given an xml data structure, but i need to pass a map to that function. the way i have described above produces extra 'map' structures when converted to json string using gson. i do not have control to change how the external service behaves (e.g. using gson to convert the map).


Mixing classes from two different JSON libraries will end in nothing but tears. And that's your issue; JSONObject is not part of Gson. In addition, trying to mix Java data structures with a library's parse tree representations is also a bad idea; conceptually an object in JSON is a map.

If you're going to use Gson, either use all Java objects and let Gson convert them, or use the classes from Gson:

JsonObject root = new JsonObject();
root.addProperty("first_name", "prod");

JsonElement element = new JsonParser().parse(complexJsonString);
root.addProperty("data", element);

String json = new Gson().toJson(root);