简体   繁体   中英

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! any idea? (the application is written in java)

thanks

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

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.

A list of uid's sounds to me like:

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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