Json4s'linq-style'for-comprehension给出空列表

时间:2013-08-20 09:29:56

标签: scala json4s

尝试从自述文件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()
  }

1 个答案:

答案 0 :(得分:2)

确定发现了问题。我们首先需要添加一个generator子句来从json

创建一个JObject
val result = for { JObject(child) <- json
                   JField("age", JInt(age))  <- child} 
             yield age
//Output : List(5, 3)