7

如何从 play 框架中的配置文件中读取用户列表?我试过做这样的事情:

users=[{uid:123,pwd:xyz},{uid:321,pwd:abc}]

从播放应用程序

 List<Object> uids = Play.application().configuration().getList("users");

会给我一个对象列表,如果我遍历列表,我会得到每个对象

{uid=123,pwd=xyz} and {uid=321,pwd=abc}

在这一点上,我不知道如何优雅地获得 uid 的值,我可以做一些 hacky 工作,省略第一个和最后一个括号并解析等号的前后,但这太难看了!任何的想法?(应用程序是用java编写的)

谢谢

4

3 回答 3

4

A Scala implementation that avoids the deprecated getConfigList method would rely on retrieving a Seq[Configuration] as follows:

  case class UserConfig(uid: Int, pwd: String)
  val userConfigs: Seq[UserConfig] = configuration.get[Seq[Configuration]]("users").map { userConfig =>
    UserConfig(userConfig.get[Int]("uid"), userConfig.get[String]("pwd"))
  }
于 2020-06-15T15:05:27.890 回答
2

对我来说,uid 的声音列表如下:

# List of UID's
users=[123,456,789] // every number represents a UID

然后你可以得到这个列表:

List<Object> uids = Play.application().configuration().getList("users");

然后用这个做你想做的事:

for (Iterator<Object> iterator = uids.iterator(); iterator.hasNext();) {        
    Object object = (Object) iterator.next();
    System.out.println(object);
}

Is this what you are looking for? BTW, you can read more about Play Framework configuration options: http://www.playframework.com/documentation/2.1.0/Configuration

于 2013-03-29T21:14:10.093 回答
2

Since I had recently the same problem and this is still unanswered, here is my suggestion:

List<User> users = getConfig().getConfigList("users").stream().map(
            config -> new User(config.getString("uid"), config.getBoolean("pwd"))
    ).collect(Collectors.toList());

As far as I know there are no tuples or anything in Java, you need to use either an object or a list with two elements. I decided to go for an object here, you can also return a list.

于 2018-10-31T14:59:28.267 回答