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

标签 json scala json4s

我有 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
}

关于json - scala json4s 我如何按条件提取字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38392572/

相关文章:

sql - 如何使用其字符串值对 JSON 数组进行排序?

scala - 使用 SBT : Invalid or corrupt jarfile 构建 Apache Spark

scala - yarn 上的 Spark 提交没有将 jar 分发到 nm-local-dir

scala - 如何使用 json4s 将对象序列化为 AST?

json - 结合类型和字段序列化器

json - 使用 json4s 从 json 字符串中提取键

javascript - 如何使用 vanilla javascript 在 Node 服务器上执行函数

java - 使用 Java 将 JSON 数组和对象转换为 TableView

javascript - JSON 和意外字符

Scalacheck:生成与生成器列表相对应的列表