gotz gotz - 1 year ago 139
JSON Question

JSON fields unordered

I am writing some RESTful services using spring MVC. I am using jsckson mapper to do the It conversions.
It all works fine except that the json it produces has fields completely unordered.

for e.g.
If my entity object looks like this:

public class EntityObj
private String x;
private String y;
private String z;

If I now have a list of EntityObjs, and I return this back from the controller, the json has the order mixed up for the fields e.g.:
[{y:"ABC", z:"XYZ", x:"DEF"},{y:"ABC", z:"XYZ", x:"DEF"}]

Looked around for a solution but not finding any. Anyone else faced this issue?

Thanks for the help

Answer Source

As others suggested, ordering should not matter. Nonetheless, if you prefer certain ordering, use @JsonPropertyOrder annotation like so:

@JsonPropertyOrder({ "x", "y", "x" })
public class EntityObj {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download