简体   繁体   English

如何从播放框架中的配置文件中读取对象列表

[英]how to read a list of objects from the configuration file in play framework

How can i read a list of users from the configuration file in play framework?如何从播放框架中的配置文件中读取用户列表? i have tried doing something like this:我试过做这样的事情:

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

from the play application从播放应用程序

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

will give me this a list of objects, if I iterate through the list i get each object as会给我一个对象列表,如果我遍历列表,我会得到每个对象

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

at this point i don't know how i can elegantly get the value of the uid, i can do some hacky job as omit the first and last bracket and parse for the before after equal sign, but it would be too ugly!在这一点上,我不知道如何优雅地获得 uid 的值,我可以做一些黑客工作,因为省略第一个和最后一个括号并在等号之后解析之前,但这太难看了! any idea?任何的想法? (the application is written in java) (应用程序是用java编写的)

thanks谢谢

A Scala implementation that avoids the deprecated getConfigList method would rely on retrieving a Seq[Configuration] as follows:避免不推荐使用的getConfigList方法的 Scala 实现将依赖于检索Seq[Configuration]如下:

  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"))
  }

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.据我所知,Java 中没有元组或任何东西,您需要使用一个对象或一个包含两个元素的列表。 I decided to go for an object here, you can also return a list.我决定在这里找一个对象,你也可以返回一个列表。

A list of uid's sounds to me like: uid 对我来说的声音列表如下:

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

Then you can get this list as:然后你可以得到这个列表:

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

And then do what you want with this:然后用这个做你想做的事:

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顺便说一句,您可以阅读有关 Play Framework 配置选项的更多信息: http : //www.playframework.com/documentation/2.1.0/Configuration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM