json - lift-json 3.0.1 将 json 数组转换为逗号分隔字符串的代码片段编译错误

标签 json scala lift-json

我正在尝试使用 json 格式的文本并将其转换为 xml。我正在使用 lift-json对于这个问题。根据 lift-json 文档 here (def toXml),我应该能够使用以下方法将 json 数组的元素转换为逗号分隔的字符串:

toXml(json map {
  case JField("nums",JArray(ns)) => JField("nums",JString(ns.map(_.values).mkString(",")))
  case x => x
})

所以我写了下面的代码:

case work: ActiveMQTextMessage => 
  println("work.getText: " + work.getText)
  val workAsJson: JValue = parse(work.getText)
  val processedArraysJson = workAsJson map {
    case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))
    case x => x
  }
  val workAsXml: scala.xml.NodeSeq = toXml(processedArraysJson)

但由于某些原因它无法编译。

它报告了两个错误:

Error:(55, 14) constructor cannot be instantiated to expected type;
 found   : net.liftweb.json.JsonAST.JField
 required: net.liftweb.json.JsonAST.JValue
        case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))

Error:(55, 49) type mismatch;
 found   : net.liftweb.json.JsonAST.JField
 required: net.liftweb.json.JsonAST.JValue
        case JField(label, JArray(ns)) => JField(label, JString(ns.map(_.values).mkString(",")))

注意,我使用的 lift-json 版本是:

"net.liftweb" % "lift-json_2.12" % "3.0.1"

使用 scala 2.12

最佳答案

这里的问题是 Lift 3.0 改变了 lift-json 对 map 用法的一些不一致。 JField 曾经是一个 JValue,但现在已经不是这样了,因为它在概念上没有意义。要映射字段,您现在必须使用 mapField。将上面代码中的 map 更改为 mapField 应该就足够了,我有 issued a PR to update the documentation

(请注意,您通常会在 the Lift Google group 上更快地得到答案。)

关于json - lift-json 3.0.1 将 json 数组转换为逗号分隔字符串的代码片段编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43100997/

相关文章:

json - Go 中的嵌套 JSON 反序列化

json - 如何从此json字段获取数据?

scala - 运行 sbt 任务启动子进程的安全方式为 `sudo`

Scala:为什么我的函数不能打印字符串参数?

scala - 使用lift-json在scala中验证json

javascript - 带选择选项的 CQ5 嵌套多字段

javascript - jQuery 到 Wordpress - Uncaught Error 让我发疯

scala - Apache Spark中的混洗与非混洗合并

scala - 使用lift-json序列化一个没有字符串作为键的 map

json - 使用lift-json进行向量反序列化