I want to convert a Seq[(String, String)]
into JSON with Scala play, but I face this error:
No Json serializer found for type Seq[(Strin
It depends on what you're trying to achieve. Say you have a Seq[(String, String)]
like this:
val tuples = Seq(("z", "x"), ("v", "b"))
If you are trying to serialize it as following:
{
"z" : "x",
"v" : "b"
}
Then just use Json.toJson(tuples.toMap)
. If you'd like to have some custom representation of your tuples you can define a Writes
as the compiler suggests:
implicit val writer = new Writes[(String, String)] {
def writes(t: (String, String)): JsValue = {
Json.obj("something" -> t._1 + ", " + t._2)
}
}
EDIT:
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))
}
}