播放框架Json输出问题 [英] Play framework Json output issue

查看:109
本文介绍了播放框架Json输出问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的操作,它输出一个json对象字符串,像这样:

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
)))

我知道了

没有发现scala.collection.immutable.Map [String,java.io.Serializable]类型的Json序列化器

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

编译错误...出了什么问题?

compilation error...what's the problem?

推荐答案

正如其他人在以前的评论中发布的那样,Map的类型不是可以通过框架反序列化为Json的东西,但是您可以轻松摆脱它地图:

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.

这篇关于播放框架Json输出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆