My JSON
looks like follow :
\"message_defaults\": {
\"LabResultsRequestDefaultMessage\": {
\"MsgTypeId\": 8,
\"StaffId\":
You can do the following without changing your json structure.
Type mapType = new TypeToken<Map<String, MessageDefault>>() {}.getType();
Map<String, MessageDefault> messagesMap = gson.fromJson(json, mapType);
List<MessageDefault> messages = new ArrayList<MessageDefault>(messagesMap.values());
If you want to get List from json first you have to correct your json. List must be in '[ ]'.
"message_defaults": [{
"LabResultsRequestDefaultMessage": {
"MsgTypeId": 8,
"StaffId": 122,
"StaffName": "Willis,Sam",
"FirstName": "Sam",
"MI": "D",
"LastName": "Willis",
"DefaultMessage": "Lab Department"
},
"ReferralRequestDefaultMessage": {
"MsgTypeId": 6,
"StaffId": 122,
"StaffName": "Willis,Sam",
"FirstName": "Sam",
"MI": "D",
"LastName": "Willis",
"DefaultMessage": "Physican"
},
"MessageComposeDefaultMessage": {
"MsgTypeId": 1,
"StaffId": 122,
"StaffName": "Willis,Sam",
"FirstName": "Sam",
"MI": "D",
"LastName": "Willis",
"DefaultMessage": "Office Manager/Willis Sam"
}
}]
EDIT : try this for converting to list
Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
Follow this link