0

I have several services that return three fields (json). FailureMessage (string, in case something wrong happens) Success (boolean) [Custom] (object of return) Examples below:

1)

{
    "FailureMessage": "",
    "Success": true,
    "Cities": []
}

2)

{
    "FailureMessage": "",
    "Success": true,
    "Regions": []
}

Is there a way to map then in a generic way?

Now, I have the first 2 properties mapped to a GenericResponse, and other classes extending GenericResponse and creating the third attribute in each one.

Thanks in advance

Felipe Diogo
  • 197
  • 1
  • 2
  • 10
  • 2
    Is [this](http://stackoverflow.com/questions/5796948/how-to-parse-dynamic-json-fields-with-gson) something that might apply to your problem? – Jonas Köritz Apr 21 '16 at 14:44
  • As @JonasKöritz said, you will use reflection. However, to decide which to parse, you will need to parse rawly like JsonNode or something to decide which class to reflect – nurgasemetey Apr 21 '16 at 15:09
  • I'd really like to avoid maps, if it can't be done, that I'd rather do it by extending common fields. – Felipe Diogo Apr 23 '16 at 18:43
  • @JonasKöritz - set your comment as answer, so I can close this question... – Felipe Diogo Apr 23 '16 at 18:49

1 Answers1

3

This should be usable as a solution to your problem.

You will have to provide gson with a method of parsing your specific structure.

Jonas Köritz
  • 2,606
  • 21
  • 33