0

So i have a json response as following:

I am trying to parse the following json response from an api. But it has a structure i do not have control over.

{
"news3962": {},
"news3961": {},
"news3960": {},
"news3959": {},
"news3958": {},
"news3951": {},
"news3950": {},
"news3948": {},
"news3943": {},
"news3947": {}
}

All the news items contain the same structure. How will i parse this response so that i return an array containing NewsItems?

Normally the response should be an array with anonoymous objects but this is not the case

Any help is greatly appreciated.

sn0ep
  • 3,843
  • 8
  • 39
  • 63
  • 1
    In Jackson I would catch this as a Map and then get the array via Map.values().toArray(new News[0]). Alas, this is not Jackson, I have no idea if this will work with Gson, and this is not an answer. just a comment. – Lev Kuznetsov Jan 29 '15 at 19:07
  • Try to parse the given JSON to Map and then take the valueSet. – Babl Jan 29 '15 at 19:07
  • 1
    This is not valid JSON; are object member keys really defined as is, that is `newsXXXX` and not `"newsXXXX"` as it should be? – fge Jan 29 '15 at 19:12
  • I am sorry you are right. @fge it does contain quotes. – sn0ep Jan 29 '15 at 19:14
  • Maybe you could solve this problem if Gson has the equivalent of `@JsonAnySetter` for Jackson; this way you could just ignore the key and inject the values in a collection – fge Jan 29 '15 at 19:25
  • @fge: yes it is. the custom gson deserializer suggested by Jonik does exactly what the OP is looking for. – njzk2 Jan 29 '15 at 19:32
  • thanks for the help guys. I found the solution to my problem see my answer below – sn0ep Jan 29 '15 at 19:36

1 Answers1

0

Thanks to Lez Kuznetsov.

I am trying to parse the response to a HashMap object. After that i take the retrieved hashmap.values().

sn0ep
  • 3,843
  • 8
  • 39
  • 63