scala> val json = parse("""
{
"name": "joe",
"addresses": {
"address1": {
"street": "Bulevard",
"city": "Helsinki"
},
"address2": {
"street": "Soho",
"city": "London"
}
}
}""")
scala> case class Address(street:String, city: String)
scala> case class PersonWithAddresses(name: String, addresses: Map[String, Address])
scala> val joe = json.extract[PersonWithAddresses]
res0: PersonWithAddresses("joe", Map("address1" -> Address("Bulevard", "Helsinki"),
"address2" -> Address("Soho", "London")))
我要訪問的joe
元素。例如,我想知道Joe的address1 city
。怎麼樣?
獎勵題: 如果PersonWithAddresses
是
case class PersonWithAddress(name:String, addresses: Map[String, List[Address]])
我將如何提取列表的大小?
P.S.問題: joe.addresses("address1").size()
和 joe.addresses.get("address1").size
有什麼區別?
'joe.addresses( 「地址1」)。city'和'joe.addresses( 「地址1」)。大小()' – Jesper
天才! 什麼joe.addresses( 「地址1」)之間'的區別。大小()'和' joe.addresses.get( 「地址1」)。size' – salvob