How to serialize Scala Map for Json in PlayFramework?

It seems that the Play Json Library cannot serialize Scala Collections. Is there an alternative. I just need to discard the data from the card.

import scala.concurrent._ import play.api.libs.ws._ import scala.concurrent.ExecutionContext.Implicits.global import play.libs.Json object temp { // Correct Serialization val javaMap = new java.util.HashMap[String, String]() javaMap.put("Abc", "Def") // Outputs: res1: String = {"Abc":"Def"} Json.stringify(Json.toJson(javaMap)) // Incorrect Serialization val scalaMap = Map("Abc" -> "Def") //> scalaMap : scala.collection.immutable.Map[String,String] = Map(Abc -> Def) // Output: res2: String = {"empty":false,"traversableAgain":true} Json.stringify(Json.toJson(scalaMap)) //> res2: String = {"empty":false,"traversableAgain":true} } 
+6
source share
1 answer

You are importing the wrong JSON library.

import play.api.libs.json._

+10
source

All Articles