0

I am using GSON to serialize Java object.

I have a Java class with following properties.

String property1;
Map<String, HashMap> property2 = new HashMap<>();
Map<String, ArrayList<String>> property3 = new HashMap<>();
Map<String, String[]> property4 = new HashMap<>();

I want to convert this to Json. Because of the maps with HashMaps inside, it has become difficult. I know I can get Json of a map with gsonObject.toJson(map). But I want all these properties in the Json Object. (all in one. not Concatenating many objects)

Can anyone help me to get this done?

4
  • What type of HashMap in your property2? Commented Mar 16, 2014 at 16:41
  • @SotiriosDelimanolis it is of <String, Integer> type. Commented Mar 16, 2014 at 16:44
  • It cannot be. <String, int> is not legal. You probably mean <String , Integer>. Also, such a set of embedded collections smells fishy... Commented Mar 16, 2014 at 16:44
  • @fge. Yes it is Integer. :-) Commented Mar 16, 2014 at 16:46

1 Answer 1

1

I don't see what the problem is. Gson can serialize Maps just fine.

Assuming your class is named Test

Test test = new Test();

test.property1 = "some value";

HashMap<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("fourty two", 42);
test.property2.put("property2-key", map);

ArrayList<String> strings = new ArrayList<>(Arrays.asList("string1",
            "string2", "string3"));
test.property3.put("property3-key", strings);

String[] stringArray = { "array1", "array2", "array3" };
test.property4.put("property4-key", stringArray);

Gson gson = new Gson();
String json = gson.toJson(test);
System.out.println(json);

It generates the following

{
    "property1": "some value",
    "property2": {
        "property2-key": {
            "fourty two": 42,
            "one": 1
        }
    },
    "property3": {
        "property3-key": [
            "string1",
            "string2",
            "string3"
        ]
    },
    "property4": {
        "property4-key": [
            "array1",
            "array2",
            "array3"
        ]
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

The property2 is of type Map<String, HashMap>. The HashMap inside it is of <String, Integer>.
@harsh That is not a problem.
It works only for simple Maps. Does not work for Maps inside Maps.
@harsh If you look at the generated JSON, you have a map inside a map in property2. property2 is a Map and it contains a key property2-key which has a value that is a Map.
It gives an error to me starting with Exception in thread "main" java.lang.IllegalArgumentException: Argument is not an array at java.lang.reflect.Array.getLength(Native Method) at com.google.gson.internal.bind.ArrayTypeAdapter.write(ArrayTypeAdapter.java:91) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:68) at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:209)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.