0

How I can convert it to POJO? I have a little troubles with "langs": {}. Is the Langs a Java object with a random quantity of variables?

Need for GsonFactory / Retrofit 2. Regards.

{
    "dirs": [
        "ru-en",
        "ru-pl",
        "ru-hu",
      ...
    ],
   "langs": {
        "ru": "russian",
        "en": "english",
        "pl": "polish",
     ...
   }

} 
Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
A. Alex
  • 35
  • 8
  • @Japu_D_Cret no, langs is like Java object with random quantity of variables: ru, en, etc. – A. Alex Mar 31 '17 at 13:40
  • so a simple Map - and now? – Japu_D_Cret Mar 31 '17 at 13:43
  • @A.Alex It is a duplicate. If you'd read the linked question best answer, you'd see the following: _Google Gson supports generics and nested beans. The [] in JSON represents an array and should map to a Java collection such as List or just a plain Java array. The {} in JSON represents an object and should map to a Java Map or just some JavaBean class._ -- exactly your case. – Lyubomyr Shaydariv Mar 31 '17 at 13:54
  • @LyubomyrShaydariv simple Map is null after GsonConverterFactory. – A. Alex Mar 31 '17 at 14:25

1 Answers1

2

Try this:

public class Example {

    @NonNull
    @SerializedName("dirs")
    public List<String> dirs;

    @NonNull
    @SerializedName("langs")
    public Map<String, String> langs;

}
shmakova
  • 6,076
  • 3
  • 28
  • 44
  • Doesn't work. Langs is null after GsonFactory. – A. Alex Mar 31 '17 at 14:05
  • @A.Alex If I'd post an answer to the question, I'd answer exactly how [shmakova](http://stackoverflow.com/users/4141049/shmakova) did: this is a correct POJO mapping. If you get `null`s for any reason -- then you can have two options here: 1) either you're trying to deserialize a JSON document that does not match the one you've posted in your question; 2) or your `Gson` instance used in Retrofit is corrupted. I don't believe the latter. – Lyubomyr Shaydariv Mar 31 '17 at 14:28
  • @LyubomyrShaydariv Thank you for help. I found the problem. Server didn't send "langs" without special parameter. – A. Alex Mar 31 '17 at 14:42
  • @A.Alex No problem. Please accept the answer since it helped you. Thanks. – Lyubomyr Shaydariv Mar 31 '17 at 14:55