scala json4s 我如何按条件提取字段

scala json4s how can i extract field by condition

我有 json(例如):

{
  "name": "",
  "count": 2,
  "children": {
    "app_open": {
      "name": "app_open",
      "count": 1,
      "children": {
        "session_end": {
          "name": "session_end",
          "count": 1,
          "children": {}
        }
      }
    },
    "app_install": {
      "name": "app_install",
      "count": 2,
      "children": {
        "session_end": {
          "name": "session_end",
          "count": 2,
          "children": {}
        }
      }
    },
    "app_instal1l": {
      "name": "app_instal1l",
      "count": 3,
      "children": {
        "app_open": {
          "name": "app_open",
          "count": 3,
          "children": {
            "session_end": {
              "name": "session_end",
              "count": 3,
              "children": {}
            }
          }
        }
      }
    }
  }
}

我需要提取 "name" = "app_open" 的所有计数。

我试着用 json4s 库来做:

val name = jsonInput filterField {
           case JField("name", "app_open") => true
           case _ => false
         }
println("name = " + URL)

我建议在输出中我会得到一些只有 "app_open" 的东西,但我得到了:

name = List((name,JString(app_open)), (name,JString(session_end)), 
(name,JString(app_open)), (name,JString(session_end)))

我做错了什么? 谢谢!

编译器错误很明显:

Error: type mismatch;
 found   : String("app_open")
 required: org.json4s.JsonAST.JValue
    case JField("name", "app_open") => true
                    ^

那是因为 type JField = (String, JValue)。像这样使用 JValue 而不是 String

val name = jsonInput filterField {
  case JField("name", JString("app_open")) => true
  case _ => false
}