3
我想要使用json格式的文本並將其轉換爲xml。對此,我正在使用lift-json。根據升降JSON文件here(def toXml
),我應該能夠使用以JSON數組的元素轉換爲逗號分隔字符串:由lift-json 3.0.1將代碼片段中的編譯錯誤轉換成逗號分隔字符串的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(",")))
通知,升JSON的版本,我使用的是:
"net.liftweb" % "lift-json_2.12" % "3.0.1"
使用Scala 2.12
非常感謝安東尼奧,這工作,我會保持你的筆記記下一倍。 – has981
沒問題!抱歉花了這麼長時間...我幾周前發現了這個問題,但忘記發佈迴應:( –