Play framework Json output issue

醉酒当歌 提交于 2019-12-25 02:55:48

问题


I have a simple action which outputs a json object string, like this:

Ok(toJson(Map(
  "results" -> result_lists
)))

This works all right. But if I do:

Ok(toJson(Map(
  "action" -> action_string, // a Scala String
  "results" -> result_lists  // a Scala List
)))

I got

No Json serializer found for type scala.collection.immutable.Map[String,java.io.Serializable]

compilation error...what's the problem?


回答1:


As others have posted in the comments before, the type of the Map is not something which can be deserialized into Json by the framework, but you can easily get rid of the Map:

scala> val s = "hello"
s: String = hello

scala> val list = List(1,2,3)
list: List[Int] = List(1, 2, 3)

scala> Json.obj("somestring" -> s, "somemap" -> list)
res0: play.api.libs.json.JsObject = {"somestring":"hello","somemap":[1,2,3]}

The resulting object can then be returned by the action as desired.



来源:https://stackoverflow.com/questions/29890354/play-framework-json-output-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!