有很多混淆是因为每个 Config 都有一个根(整个对象的根),但是你的层次结构的顶部也称为根,我们说的是两个不同的根。这是 scala shell 摘录,它说明了正在发生的事情:
res75: com.typesafe.config.Config = Config(SimpleConfigObject({"root":{"value1":1,"value2":2,"values":{"dynamic1":{"static1":10,"static2 ":"test"},"dynamic2":{"static1":10,"static2":"test"}}}}))
scala> cfig.root()
res74: com.typesafe.config.ConfigObject = SimpleConfigObject({"root":{"value1":1,"value2":2,"values":{"dynamic1":{"static1":10,"static2":"test"},"dynamic2":{"static1":10,"static2":"test"}}}})
scala> val c2 = cfig.getObject("root")
c2: com.typesafe.config.ConfigObject = SimpleConfigObject({"value1":1,"value2":2,"values":{"dynamic1":{"static1":10,"static2":"test"},"dynamic2":{"static1":10,"static2":"test"}}})
scala> c2.entrySet.size
res72: Int = 3
scala> c2.keySet.toSet
res73: scala.collection.immutable.Set[String] = Set(value2, value1, values)