json - 在 Scala 中的 JsObject 中查找 JsValue
问题描述
如何创建一个在 JsObject 中找到 JsValue 的递归函数,例如:函数输入:JsObject 和路径作为字符串示例:
JsObject 的输入:
{
"name": "Zvi"
parents: {
"father": {
"name": "myFatherName",
"dob": "10/10/70"
}
}
}
和路径示例:
- path = notExistsField 输出将为 None
- path = name 输入将是 Some("Zvi")
- path = parents 输出将是
Some({
"father": {
"name": "myFatherName",
"dob": "10/10/70"
}
})
- parents.father 的输出将是:
Some({
"name": "myFatherName",
"dob": "10/10/70"
})
- parents.father.name 输出将是“myFatherName”
测试示例:
"deepSearch" - {
val payload: JsObject = Json.parse(
"""{
| "key1": 1,
| "key2": {
| "key1": "value21",
| "key2": {
| "key1": 221
| }
| },
|}""".stripMargin).as[JsObject]
"found" - {
"nested" in {
// In Nested Object
service.deepSearch(payload,"key2.key1").get shouldBe JsString("value21")
service.deepSearch(payload,"key2.key2.key1").get shouldBe JsNumber(221)
service.deepSearch(payload,"key2.key2").get shouldBe Json.parse("{\"key1\": 221}")
}
"top-level" in {
service.deepSearch(payload,"key1").get shouldBe JsNumber(1)
}
}
"not found" - {
"nested" in {
service.deepSearch(payload,"key2.key2.key1.notExists").isEmpty shouldBe true
service.deepSearch(payload,"key2.key2.notExists").isEmpty shouldBe true
}
"top-level" in {
service.deepSearch(payload,"Boom").isEmpty shouldBe true
}
}
}
解决方案
我做了以下实现,有没有更漂亮的代码的建议
def deepSearch(payload: JsValue, path: String): JsLookupResult = {
path indexOf (".") match {
// For the Base case:
case base if base < 0 => (payload \ path)
// For the Step case:
case found if found >= 0 =>
val untilFirstDot = path.substring(0, found)
(payload \ untilFirstDot) match {
case JsDefined(newPayload) => deepSearch(newPayload, path.substring(found + 1))
case undefined: JsUndefined => undefined
}
}
}
推荐阅读
- react-admin - React Admin - 获取表单中的当前值
- postgresql - 使用 postgresQL 选择具有首先出现的 id 的行
- regex - 使用 Groovy 脚本从日志文件中复制特定的 XML 块
- java - 访问 REST API 时完全忽略 WAR 名称
- plugins - 自定义注释和标记工具栏以在 acrobat 2017 中添加一些按钮
- javascript - 带有 OneMap 的 Leaflet.js:标记和多边形问题
- c# - 布尔的 PropertyBuilder
- mysql - 按年和月分组数据
- graph - Cacti - 数据源模板更改后轮询器停止 - 旧但黄金
- rsa - 导入 RSA 密钥 - 找不到指定的文件