我正在使用 json4s 库将 scala 中的 map 转换为 json,但在 Map[Char,Int] 上操作时一直遇到基本错误:
这是给我带来问题的代码示例。
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL.WithDouble._
val myMap = Map('a' -> 123)
render(myMap)
错误:(Char, Int) => org.json4s.JsonAST.JValue 中没有可用的隐式 View 。
Question: What is the correct way to convert a Map that is made of [Char, Int] to a Json object using Json4s?
最佳答案
JSON 对象的键总是字符串,而且在 JSON 中没有 Char
的等价物。参见 json.org用于规范。
您可以在渲染之前转换您的 Map[Char, Int]
的键:
myMap.map { case(k, v) => (k.toString, v) }
关于json - 如何使用Json4s将Map转Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24116536/