2

I have to consume a JSON Rest api (for mobile). This api returns a field that is changing like so : (string, int, null, array, object or array of object)

{"field": [{"id": 12, "value": "string value"}]} //array of object
{"field": 12345} //int
{"field": "string"} //string
{"field": {"id": 1, "value": "I'm an object now"}} //object
{"field": ["array", "of", "string"]} //array of string

I may be possible to change the server response to be able to do some standardisation, which could be great! Right now I had to create an adapter (with GSON) but this solution have is limitations, especially with performances and maintainability..

How could I create a good response that will be easy to understand and use?

Community
  • 1
  • 1
Maurice
  • 2,129
  • 2
  • 25
  • 33
  • check this [answer](https://stackoverflow.com/a/46320656/8009433) – Navneet Krishna Jun 21 '18 at 09:06
  • What's the problem? You want to change the format of the response json, on the server side? – Hong Duan Jun 21 '18 at 09:10
  • Yes I think so, I want to parse easily the response. An I don't know what to do to have an easy and understandable api. On the client side, I have to use a Gson adapter, which is very hard to maintain and create. – Maurice Jun 21 '18 at 09:19

2 Answers2

0

You could find some relevant information here :

There isn't any json specifications, everybody can do whatever they want. But there are some best practices.

0

The simple & useful structure is as below:

{
  "success": true,
  "message": "It is done",
  "data": {
    "id": 123,
    "value": "string",
    "object":{
            "object_value":"I'm an object now"
             },
    "array_object":[ {"key1":"value1"},{"key2":"value2"},{"key3":"value3"} ]

  },
  "string": "Hello World"
}