Why does this json4s code work in the scala repl but fail to compile?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Why does this json4s code work in the scala repl but fail to compile?相关的知识,希望对你有一定的参考价值。
I‘m converting a json-like string into json, and the following code works in the scala repl
import org.json4s._
import org.json4s.JsonDSL._
import org.json4s.JsonDSL.WithDouble._
import org.json4s.native.JsonMethods._
val value ="{100:1.50;500:1.00;1000:0.50}"val data = value.stripPrefix("{").stripSuffix("}").split(";").map(a =>{val b = a.split(":")(b(0),b(1))}).toMap
compact(render(data))
But when it is compiled, I‘m getting the following error
render() is imported from JsonMethods._ and it actually requires a JValue. You have imported an implicit map2jvalue twice from those two imports import org.json4s.JsonDSL._ and import org.json4s.JsonDSL.WithDouble._.
I suspect that the compiler didn‘t find the implicit due to the ambiguous imports, try to be more selective: the 3rd import seems redundant (the one with JsonDSL.WithDouble._).
Sometimes you can run scalac with -Xlog-implicits to see why implicits are not used.
以上是关于Why does this json4s code work in the scala repl but fail to compile?的主要内容,如果未能解决你的问题,请参考以下文章