json4s 预期的 JValue (String,String) 给定

json4s JValue expected (String,String) given

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

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

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

事实上,我已经在 J​​Object 中有了字符串,在 Array[String] 中有了字符串列表(但如果需要,可以将其更改为列表)。所以我遵循了 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 (杰克逊)

您必须另外导入一些隐式转换方法:

import org.json4s.JsonDSL._

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