我有以下JSON:
{
"key1":[
{
"key2":{
"key3":"value3",
"key4":"value4"
},
"key5":{
"key6":"value6"
},
"key7":[
{
"key8":"value8",
"key9":"value9"
}
],
"key10":"value10",
"key11":"value11"
}
],
"key12":"value12"
}
如何使用SprayJson检索嵌套元素(例如 value6 )。
我设法只检索了顶级键“ key1” 。
case class Key1(key1: JsArray)
object Key1Protocol extends DefaultJsonProtocol {
implicit val key1: RootJsonFormat[Key1] = jsonFormat1(Key1)
}
<jsonString>.parseJson.convertTo[Key1]
答案 0 :(得分:2)
这可以帮助:
case class Key1(key1: JsArray)
object Key1Protocol extends DefaultJsonProtocol {
implicit val key1: RootJsonFormat[Key1] = jsonFormat1(Key1)
}
import spray.json._
import DefaultJsonProtocol._
object MainJson {
import Key1Protocol._
def main(args: Array[String]): Unit = {
val jsonAst = TestComplexJson.str.parseJson.convertTo[Key1]
val result = jsonAst.key1
.elements(0)
.asJsObject
.getFields("key5")(0)
.asJsObject()
.getFields("key6")(0)
println(result)
}
}
给出“ value6”