Json serial parser not found for type Seq [(String, String)]. Try to implement implicit Writes or Format for this type

I want to convert Seq[(String, String)]to JSON with a Scala game, but I am encountering this error:

There is no Json serializer for the type Seq [(String, String)]. Try to implement implicit Writes or Format for this type.

How can i fix this?

+4
source share
2 answers

It depends on what you are trying to achieve. Say you have Seq[(String, String)]:

val tuples = Seq(("z", "x"), ("v", "b"))

If you are trying to serialize it like this:

{
  "z" : "x",
  "v" : "b"
}

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

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))
  }
}
+10

Scala 2- JSON ( ), Writes 2-Tuple (A, B). (String, String), !

implicit def tuple2Writes[A, B](implicit w1: Writes[A], w2: Writes[B]): Writes[(A, B)] = new Writes[(A, B)] {
  def writes(tuple: (A, B)) = JsArray(Seq(w1.writes(tuple._1), w2.writes(tuple._2)))
}

, . (.. ).

+1

All Articles