简体   繁体   中英

How do I convert a Scala Map consisting of String and Seq[String] values to JSON?

In Play Framework using Scala, how do I convert a Map of String and Seq[String] values to JSON format? The following code produces an error for me:

val objMap = Map("id" -> "id", "tags" -> Seq("tag1", "tag2"))
Json.toJson(objMap)

The resulting error:

No Json serializer found for type scala.collection.immutable.Map[String,Object]. Try to implement an implicit Writes or Format for this type.

As the error says you'll need to implement a Writes for Map[String,Object]

here is what it will look like. I have compiled this code and it looks fine. (I assume you're using Play framework). FYI: cases in there are only for starting point. you can add/modify more case s in there based on your requirements.

import play.api.libs.json._
import play.api.libs.json.Writes
import play.api.libs.json.Json.JsValueWrapper

val objMap = scala.collection.immutable.Map("id" -> "id", "tags" -> Seq("tag1", "tag2"))
Json.toJson(objMap)

implicit val writeAnyMapFormat = new Writes[Map[String, Object]] {
def writes(map: Map[String, Object]): JsValue = {
  Json.obj(map.map {
    case (s, a) => {
      val ret: (String, JsValueWrapper) = a match {
        case _: String => s -> JsString(a.asInstanceOf[String])
        case _: java.util.Date => s -> JsString(a.asInstanceOf[String])
        case _: Integer => s -> JsString(a.toString)
        case _: java.lang.Double => s -> JsString(a.toString)
        case None => s -> JsNull
        case JsArray(elements) => s -> JsArray(elements)
        case _ => s -> JsArray(a.asInstanceOf[List[Int]].map(JsNumber(_)))
      }
      ret
    }
  }.toSeq: _*)

}
}

You can simply do this:

val objMap = Map("id" -> Json.toJson("id"), "tags" -> Json.toJson(Seq("tag1", "tag2")))
Json.toJson(objMap)

Will result in this:

 {"id":"id","tags":["tag1","tag2"]}

Can you not change the map to

val objMap = Map("id" -> Seq("id"), "tags" -> Seq("tag1", "tag2"))

OR

You could define a case class and an implicit writes, it is easier to define for a case class

case class Post(id: String, tags: Seq[String])

implicit val postWrites = Json.writes[Post]

val objMap = Seq(Post("id1", Seq("tag1", "tag2")))

Json.toJson(objMap)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM