[英]How to unmarshall JSON object with multiple value types
Say I have an object: 说我有一个对象:
{
"name": "joe",
"age": 60
}
How to unmarshall it into a Map[String, String]
type? 如何将其解组为
Map[String, String]
类型?
The JsObject
class has a fields
parameter that is a Map[String, JsValue]
. JsObject
类有一个fields
参数,它是Map[String, JsValue]
。 If you want a Map[String, String]
, use mapValues
: 如果你想要
Map[String, String]
,请使用mapValues
:
import spray.json._
import DefaultJsonProtocol._
val json =
"""{
"name": "joe",
"age": 60
}"""
val jsObj = json.parseJson.asJsObject // JsObject
val myMap: Map[String, String] = jsObj.fields.mapValues(_.toString)
println(myMap)
// Map(name -> "joe", age -> 60)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.