【问题标题】:Json4s 'linq-style' for-comprehension giving empty listJson4s 'linq-style' 用于理解给出空列表
【发布时间】:2013-08-20 09:29:56
【问题描述】:

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

【问题讨论】:

    标签: scala json4s


    【解决方案1】:

    好的,找到问题了。我们首先需要添加一个生成器子句来从 json 中创建一个 JObject

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2015-12-02
      • 2017-04-01
      相关资源
      最近更新 更多