1

I have JSON that may look like this

  {
     "endpoint": {
         "status": "xxxx",
         "port": [ 123 ],
     }
  }

or this

{
     "endpoint": {
         "status": "xxxxxxx",
         "port": 456,
     }
}

I have class

public class Endpoint {
    private String status;
    private List<Integer> port;
}

in my code

    Endpoint endpoint = gson.fromJson(jsonString, Endpoint.class);

I got error like so:

Expected BEGIN_ARRAY but was NUMBER 

I know the problem with de-serialization that port field's type is either returned as Integer or List of Integer, which I have no control over and have to consume as is.

How to properly de-serialize using Gson in this situation? is custom de-serialization way to go? if so I'd appreciate if someone can give an example. Thanks.

yantaq
  • 3,968
  • 2
  • 33
  • 34

0 Answers0