【问题标题】:Using JsonPath from scala to extract full field : value list使用 scala 中的 JsonPath 提取完整字段:值列表
【发布时间】:2021-08-05 13:58:16
【问题描述】:

我正在尝试从 json 文档中获取一组完全限定的 path : value 对。

即给定

{"a":"b", "c":{"d":3"}}

我愿意

a :: "b"
c.d :: 3

或精神上相似的东西。似乎有一个 java 库声称可以做到这一点:

import $ivy.`com.jayway.jsonpath:json-path:2.6.0`
import com.jayway.jsonpath.Configuration
import com.jayway.jsonpath.Option
import com.jayway.jsonpath.JsonPath._

val conf = com.jayway.jsonpath.Configuration.defaultConfiguration();
val pathList = using(conf).parse("""{"a":"b", "c":{"d":3}}""")
val arg = pathList.read("$..id")

我收到此错误

java.lang.ClassCastException: net.minidev.json.JSONArray cannot be cast to scala.runtime.Nothing$
    at repl.MdocSession$App.<init>(json test.worksheet.sc:38)
    at repl.MdocSession$.app(json test.worksheet.sc:3)

有什么想法吗?

【问题讨论】:

  • 如果你想在 Scala 中编码,有几个更好/更惯用的 json 库...... Duckduckgo 是你的朋友

标签: scala jsonpath


【解决方案1】:
val arg = pathList.read[net.minidev.json.JSONArray]("$..*")

需要演员表...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2018-08-01
    相关资源
    最近更新 更多