2013-08-20 55 views
2

嘗試從自述文件https://github.com/json4s/json4s#linq-style 中的示例運行以下測試,但我得到一個空列表。我沒有在示例中指定的結果列表(5,3)Json4s'linq-style'爲理解提供空列表

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() 
    } 

回答

2

確定找到了問題。我們首先需要添加一個generator子句來創建json的JObject

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