0

I have the following code:

ArrayList<HashMap<String,String>> arr = new ArrayList<HashMap<String,String>>();
arr.add(new HashMap<String, String>(){{
        put("title","123");
        put("link","456");
}});
print(arr.toString());
print(new Gson().toJson(arr));

and I get the following output:

[{link=456, title=123}]
[null]

But I hope it is:

[{link=456, title=123}]
[{"title":"123","link":"456"}] //Serialize ArrayList<HashMap> via GSON

I searched a lot of posts, I have no idea. thanks for any response.

neslxzhen
  • 75
  • 1
  • 9
  • Please **avoid double-brace-initializer**. It is a *dirty hack*. Use `Map.of` if you do not have to modify the map (looks like it) or just put it manually into the map. – Zabuzard May 07 '20 at 10:58
  • GSON probably doesnt know how to deal with your anonymous class. Are you familiar with how **double-brace-initializer** works? You are not giving it a `HashMap` but an anonymous subclass of it. Get rid of it, maybe it works then. – Zabuzard May 07 '20 at 11:00
  • that right! thanks @Zabuzard and I got more information from here: https://stackoverflow.com/questions/6802483/how-to-directly-initialize-a-hashmap-in-a-literal-way – neslxzhen May 07 '20 at 18:21
  • Sooo, does it work now that you got rid of double-brace-initializer and used a more regular way of creating a map which doesnt involve a subclass? Also note that GSON instances are typically build using `GsonBuilder`. – Zabuzard May 07 '20 at 18:36

2 Answers2

0

Please try some alternate solution i have.

  1. First one uses only JsonObject and JsonArray from Gson package.
private static void toJsonElement(String title, String link) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("title", title);
        jsonObject.addProperty("link", link);
        JsonArray jsonArray = new JsonArray();
        jsonArray.add(jsonObject);
        System.out.println(jsonArray);
}
  1. Uses JsonObject with ArrayList
private static void toArrayList(String title, String link) {
        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("title", title);
        jsonObject.addProperty("link", link);
        List<JsonObject> listOfObject = new ArrayList<>();
        listOfObject.add(jsonObject);
        System.out.println(new Gson().toJson(listOfObject)); // listOfObject.toString() also works
}
silentsudo
  • 6,730
  • 6
  • 39
  • 81
0

Use TypeToken to get the type

Gson uses Java reflection API to get the type of the object to which a Json text is to be mapped. But with generics, this information is lost during serialization. To counter this problem, Gson provides a class com.google.gson.reflect.TypeToken to store the type of the generic object.

For example :

ArrayList<HashMap<String, String>> arr = new ArrayList<>();
arr.add(new HashMap<String, String>() {{
    put("title", "123");
    put("link", "456");
}});
System.out.println(arr.toString());

Type type = new TypeToken<ArrayList<HashMap<String, String>>>() {}.getType();
System.out.println(new Gson().toJson(arr, type));

Output :

[{link=456, title=123}]
[{"link":"456","title":"123"}]
Ashraful Islam
  • 12,470
  • 3
  • 32
  • 53