Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gson deserialize into map

I have this json string which I need to get deserialized into a map: Map

"players_test": [
    {
        "54231f85f8e049c7bd8ac0aba3d1caf7": {
            "uuid": "54231f85f8e049c7bd8ac0aba3d1caf7",
            "name": "TomShar",
            "signup_time": "2014-07-04 16:27:16"
        }
    },
    {
        "54231f85f8e049c7bd8ac0aba3d1caf7": {
            "uuid": "54231f85f8e049c7bd8ac0aba3d1caf7",
            "name": "TomShar",
            "signup_time": "2014-07-04 16:27:16"
        }
    },
    {
        "54231f85f8e049c7bd8ac0aba3d1caf7": {
            "uuid": "54231f85f8e049c7bd8ac0aba3d1caf7",
            "name": "TomShar",
            "signup_time": "2014-07-04 16:27:16"
        }
    }
]

So the Strings should be the keys and then the value should be of the object it represents. I have a custom deseriaziler written for the UUID object and that is tested and works (so that isn't the problem).

EDIT:

I found a better JSON structure I can use for my problem that works exactly how I want it to.

"players": {
    "54231f85-f8e0-49c7-bd8a-c0aba3d1caf7": {
        "uuid": "54231f85-f8e0-49c7-bd8a-c0aba3d1caf7",
        "name": "TomShar",
        "kills": 0,
        "assists": 0,
        "damage_dealt": 0,
        "time_alive": 0,
        "dead": false
    },
    "KEY": {
        "uuid": "KEY",
        "name": "Name",
        "kills": 0,
        "assists": 0,
        "damage_dealt": 0,
        "time_alive": 0,
        "dead": false
    },
    "KEY": {
        "uuid": "KEY",
        "name": "Name",
        "kills": 0,
        "assists": 0,
        "damage_dealt": 0,
        "time_alive": 0,
        "dead": false
    }
}
like image 717
TomShar Avatar asked Jul 15 '14 17:07

TomShar


People also ask

How to convert JSON to HashMap using Gson?

String jsonString = "{'employee.name':'Bob','employee. salary':10000}"; Gson gson = new Gson(); Map map = gson. fromJson(jsonString, Map. class); Assert.

Is Gson better than Jackson?

Conclusion Both Gson and Jackson are good options for serializing/deserializing JSON data, simple to use and well documented. Advantages of Gson: Simplicity of toJson/fromJson in the simple cases. For deserialization, do not need access to the Java entities.

Which of the following Gson methods are useful for implementing JSON?

Gson is first constructed using GsonBuilder and then, toJson(Object) or fromJson(String, Class) methods are used to read/write JSON constructs.


1 Answers

First, enclose the JSON string inside {...}, then you can easily convert it into Map as shown below:

class PlayerObject {
    private String uuid;
    private String name;
    private String signup_time;
    // getters & setters
}

Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<Map<String, PlayerObject>>>>(){}.getType();
Map<String, ArrayList<Map<String, PlayerObject>>> map = gson.fromJson(jsonString, type);

Is it possible to have the map like: Map<String, PlayerObject> players?

Yes, you can convert it into desired format as shown below:

Map<String,PlayerObject> players=new HashMap<String,PlayerObject>();
for(Map<String, PlayerObject> m:map.get("players_test")){
   for(String key:m.keySet()){
       players.put(key, m.get(key));
   }
}
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(players));
like image 143
Braj Avatar answered Oct 04 '22 13:10

Braj