尝试从自述文件https://github.com/json4s/json4s#linq-style中的示例运行以下测试 但我得到一个空列表。我没有在示例
中指定的结果List(5,3) test("JValue with for comprehension") {
import org.json4s._
import org.json4s.native.JsonMethods._
val json = parse( """
{ "name": "joe",
"children": [
{
"name": "Mary",
"age": 5
},
{
"name": "Mazy",
"age": 3
}
]
}
""")
val result = for {JField("age", JInt(age)) <- json} yield age
println(result)
//Output : List()
}
答案 0 :(得分:2)
确定发现了问题。我们首先需要添加一个generator子句来从json
创建一个JObjectval result = for { JObject(child) <- json
JField("age", JInt(age)) <- child}
yield age
//Output : List(5, 3)