遍历播放框架中的对象列表

Iterating through an Object list in play framework

本人初玩框架

我写了这段 java 代码来获取用户的详细信息

List<Person> personsDetails = new Model.Finder(String.class, Person.class).all();

这是我的 json 字符串

[{"id":"1","email":"xyz@gmail.com","password":"zxcv","login_method":"2"},
{"id":"2","email":"bvcx@gmail.com","password":"abcdef","login_method":"3"}]

如何通过ID获取特定用户?

方法一: 你需要迭代你的列表 personsDetails 并检查特定的 id,每次你想通过 id 获取特定的人。


方法二: 你可以准备Map of id and Person -> Map<Integer, Person>。并使用 map.get(Object key) 方法通过 id 获取特定的人。

使用此代码通过 ID 获取特定用户。

List<Person> personsDetails = new Model.Finder(String.class,Person.class).all();

HashMap<String, Person> personMap = new HashMap<String, Person>();

for (Person product : personsDetails) {
      if (product.id == id ) {
            personMap.put("CustomerAccount", product);
      }
}

用生成的新哈希映射做任何你想做的事