【问题标题】:json4s JValue expected (String,String) givenjson4s JValue 预期(字符串,字符串)给定
【发布时间】:2017-04-27 10:09:09
【问题描述】:

使用 Scala 和 json4s(也许我缺少金鱼库什么的)

我正在尝试将一些字符串列表(或数组)添加到 JSON 中,所以最终看起来像:

{"already":"here",..."listToAdd":["a","b",c"]}

事实上,我已经在 J​​Object 中拥有了字符串,并且在 Array[String] 中拥有了字符串列表(但如果需要,可以将其更改为 List)。所以我关注了docat json4s.org,它指出:

Any seq produces JSON array.

scala> val json = List(1, 2, 3)

scala> compact(render(json))

res0: String = [1,2,3]

Tuple2[String, A] produces field.

scala> val json = ("name" -> "joe")

scala> compact(render(json))

res1: String = {"name":"joe"}

当尝试它时,它会给出:

Error:(15, 28) type mismatch;
found   : (String, String)
required: org.json4s.JValue
    which expands to)  org.json4s.JsonAST.JValue
    println(compact(render(idJSON)))

使用 Scala 2.11.4 Json4s 3.2.11(杰克逊)

【问题讨论】:

    标签: json scala jackson json4s


    【解决方案1】:

    你必须额外导入一些隐式转换方法:

    import org.json4s.JsonDSL._
    

    这些会将 Scala 对象转换为库的 Json AST。

    【讨论】:

    • 非常感谢!我错过了那个!
    猜你喜欢
    • 1970-01-01
    • 2017-08-08
    • 2020-01-31
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 2015-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多