[英]How to convert Map to Json using Json4s
I am using the json4s library to convert maps in scala to json but keep running into a basic error when operating on Map[Char,Int]: 我正在使用json4s库将scala中的地图转换为json,但是在对Map [Char,Int]进行操作时始终遇到基本错误:
Here is the code sample that is giving me problems. 这是给我问题的代码示例。
import org.json4s.jackson.JsonMethods._
import org.json4s.JsonDSL.WithDouble._
val myMap = Map('a' -> 123)
render(myMap)
error: No implicit view available from (Char, Int) => org.json4s.JsonAST.JValue. 错误:(Char,Int)=> org.json4s.JsonAST.JValue没有可用的隐式视图。
Question: What is the correct way to convert a Map that is made of [Char, Int] to a Json object using Json4s?
问题:使用Json4s将[Char,Int]组成的Map转换为Json对象的正确方法是什么?
The keys of a JSON object are always strings, and furthermore, there is no equivalent of Char
in JSON. JSON对象的键始终是字符串,而且,JSON中没有等效的
Char
。 See json.org for the specification. 有关规范 ,请参见json.org 。
You could convert the keys of your Map[Char, Int]
before rendering: 您可以在渲染之前转换
Map[Char, Int]
的键:
myMap.map { case(k, v) => (k.toString, v) }
Also you could consider to use 您也可以考虑使用
``println(scala.util.parsing.json.JSONObject(m))``` ``调用println(scala.util.parsing.json.JSONObject(米))```
From Scala 2.10 从Scala 2.10开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.