我是Scala的新手,我正在学习Scala和Play框架:我正在尝试使用Map(...)、List(...)和Json.toJson(...)从名为"tables“的数据序列开始,使用play/scala动态创建Json。我的结果应该类似于下面所示的代码resultCustomJsonData
var resultCustomJsonData = [
  {
    text: "Parent 1",
    nodes: [
      {
        text: "Child 1",
        nodes: [
          {
            text: "Grandchild 1"
          },
          {
            text: "Grandchild 2"
          }
        ]
      },
      {
        text: "Child 2"
      }
    ]
  },
  {
    text: "Parent 2"
  },
  {
    text: "Parent 3"
  },
  {
    text: "Parent 4"
  },
  {
    text: "Parent 5"
  }
];我的scala代码如下:
val customJsonData = Json.toJson(
      tables.map { t => {
        Map(
          "text" -> t.table.name.name, "icon" -> "fa fa-cube", "nodes" -> List (
              Map( "text" -> "properties" )
          )
        )
      }}
    )但是我得到了这个错误:
No Json serializer found for type Seq[scala.collection.immutable.Map[String,java.io.Serializable]]. Try to implement an implicit Writes or Format for this type.发布于 2019-06-26 05:00:49
这里有一种不使用临时Map的方法
import play.api.libs.json.Json
val customJsonData = Json.toJson(
      tables.map { t =>  
        Json.obj( 
         "text" -> t.table.name.name, 
         "icon" -> "fa fa-cube",
         "nodes" -> Json.arr(Json.obj("text" -> "properties"))
         )
      } 
    )https://stackoverflow.com/questions/56761330
复制相似问题