Home > Mobile >  List is not in order
List is not in order

Time:07-09

I converted UUID to string (String id) and put the conversion inside a method.

I also declared other String variables such as FirstName etc and put in on an ArrayList: img2

Code

The code does work. But I'm confused why the string email was showing second on the list.

public class StudentController {
    @Autowired
    StudentService studentService = new StudentService();

    @GetMapping
    public List<Student> displayStudent(){
        return studentService.getStudent();
    }
}

public class StudentService {
    Student student = new Student();
    private List<Student> studentList = Arrays.asList(
        new Student(student.genID(),"Elvis" , "Presley" ,"[email protected]")
    );
    
    public List<Student> getStudent(){
        return studentList;
    }
}

public class Student {
    UUID uuid = UUID.randomUUID();
    private String id;
    private String FirstName;
    private String LastName;
    private String email;

    public Student() {}

    //Method Converting UUID into string
    public String genID(){
        id = uuid.toString();
        return id;
    }

    public Student(String id) {
        this.id = id;
    }
    
    public Student(String id, String firstName, String lastName, String email) {
        this.id = id;
        FirstName = firstName;
        LastName = lastName;
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return FirstName;
    }

    public void setFirstName(String firstName) {
        FirstName = firstName;
    }

    public String getLastName() {
        return LastName;
    }

    public void setLastName(String lastName) {
        LastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}

Expected

I expected data to be in this order

ID , FirstName , LastName , email

Actual Output JSON

img1

CodePudding user response:

JSON is an unordered collection, as specified on https://www.json.org/json-en.html , so you don't have to worry about it. It might depend on library though.

CodePudding user response:

Specify the serialized order of properties

The order of properties during serialization can be defined in Jackson. Either at class-level specifically using annotation @JsonPropertyOrder. Or globally for your ObjectMapper using a feature:

objectMapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true)

Example

In your case you can achieve expected order using the annotation on your class:

@JsonPropertyOrder({'id', 'firstName', 'lastName', 'email'})
public class Student {
  // body of your class
}

Or separately with an index on your fields:

public class Student {
  @JsonProperty(index=10) 
  private String id;

  // not ordered specifically
  private String firstName;
  private String lastName;

  @JsonProperty(index=20) 
  private String email;

  // remainder of your class
}

See also

  • Related