解析的一种方法是映射根JSON对象的子元素:
scala> parse(res).children.map(_.extract[Person])
res3: List[Person] = List(Person(Name One,Address(Some Street,Some City)), Person(Name Two,Address(Some Other Street,Some Other City)))
如果需要保留字段名,请这样做:
scala> Map() ++ parse(res).children.map { case f: JField => (f.name, f.extract[Person]) }
res4: scala.collection.immutable.Map[String,Person] = Map(person1 -> Person(Name One,Address(Some Street,Some City)), person2 -> Person(Name Two,Address(Some Other Street,Some Other City)))
注意,当我们开始使用2.8功能时,以下直接方法应该有效:
parse(res).extract[Map[String, Person]]