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