1

I have the below code which I want to serialize.

class .. {
  public transient long id;
  public Date steps;
  public int days;
  public List<Integer> next;
}

I output this to JSON string using the below code.

String json = new Gson().toJson(obj)

The output which I get is as under

{"days":2, "next": [3,5,8], "steps": "Nov 24, 2020 9:50:09 PM"}

As you can see, it is sorting output in alphabetic order. I do no want this. I want it to be in order I defined in the class i.e.

{"steps": "Nov 24, 2020 9:50:09 PM", "days":2, "next": [3,5,8]}

How can this be achieved ?

SimpleGuy
  • 2,764
  • 5
  • 28
  • 45
  • I think this will answer your question: https://stackoverflow.com/q/6365851/8298909. It looks like the answer is "it's not possible unless you write your own serializer or use a different library". (Edit: oh, hm, I didn't see the android tag and so I didn't expect to be able to one-vote close this question.) – Ben P. Nov 24 '20 at 16:46

0 Answers0