lot*_*tfi 4 json scala playframework-2.2
我想Seq[(String, String)]用Scala播放转换成JSON,但是我遇到了这个错误:
找不到类型为Seq [(String,String)]的Json序列化程序.尝试为此类型实现隐式写入或格式.
我怎样才能解决这个问题?
ser*_*jja 11
这取决于你想要达到的目标.说你有Seq[(String, String)]这样的:
val tuples = Seq(("z", "x"), ("v", "b"))
Run Code Online (Sandbox Code Playgroud)
如果您尝试将其序列化如下:
{
"z" : "x",
"v" : "b"
}
Run Code Online (Sandbox Code Playgroud)
然后就用吧Json.toJson(tuples.toMap).如果您想要对元组进行自定义表示,可以Writes按照编译器的建议定义:
implicit val writer = new Writes[(String, String)] {
def writes(t: (String, String)): JsValue = {
Json.obj("something" -> t._1 + ", " + t._2)
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
import play.api.mvc._
import play.api.libs.json._
class MyController extends Controller {
implicit val writer = new Writes[(String, String)] {
def writes(t: (String, String)): JsValue = {
Json.obj("something" -> t._1 + ", " + t._2)
}
}
def myAction = Action { implicit request =>
val tuples = Seq(("z", "x"), ("v", "b"))
Ok(Json.toJson(tuples))
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7882 次 |
| 最近记录: |