6

I don't know if it is possible to deserialise arrays into hashMap i have got json :

"additionalProperties": [
{
  "$type": "Tfl.Api.Presentation.Entities.AdditionalProperties, Tfl.Api.Presentation.Entities",
  "category": "Description",
  "key": "TerminalName",
  "sourceSystemKey": "BikePoints",
  "value": "200017",
  "modified": "2016-05-19T12:02:39.82"
}
........
]

and for that json i have got list :

private List<AdditionalProperties> additionalProperties;

everything works perfect but how store that json in HashMap where Key is "key" (TerminalName)"

private HashMap<String,AdditionalProperties> additionalProperties;
Łukasz Woźniczka
  • 1,625
  • 3
  • 28
  • 51
  • So that first object in the JSON array for `additionalProperties` is one `AdditionalProperties` object? And you want to pull out the String for `key` and make that the key for the whole object in your `Map`? If that's the case the simplest way would probably be to create an intermediate object and transform that with with `@ToJson` and `@FromJson` annotated methods. Have a look at https://github.com/square/moshi/blob/master/README.md#another-example – david.mihola May 20 '16 at 14:28

1 Answers1

24

Moshi supports fields declared as Map but not as HashMap. This way Moshi can use a different implementation of Map that’s more appropriate than HashMap for decoded JSON. If you change your field’s type to Map<String,AdditionalProperties> it should work.

Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128