如何在scala中将映射转换为Json

如何在scala中将映射转换为Json,json,scala,playframework,Json,Scala,Playframework,如何在scala中将以下Map结构(Map[String,Any])转换为Json?我在玩游戏 val result = s .groupBy(_.dashboardId) .map( each => Map( "dashboardId" -> each._1, "cubeId" -> each._2.head.cubeid, "dashboardName" -> each._2.head.dashboardName, "reports"

如何在scala中将以下Map结构(Map[String,Any])转换为Json?我在玩游戏

  val result =  s
  .groupBy(_.dashboardId)
  .map(
  each => Map(
  "dashboardId" -> each._1,
  "cubeId" -> each._2.head.cubeid,
  "dashboardName" -> each._2.head.dashboardName,
  "reports" -> each._2.groupBy(_.reportId).map(
    reportEach => Map(
      "reportId" -> reportEach._1,
      "reportName" -> reportEach._2.head.reportName,
      "selectedColumns" -> reportEach._2.groupBy(_.selectedColumnid).map(
        selectedColumnsEach => Map(
          "selectedColumnId" -> selectedColumnsEach._1,
          "columnName" -> 
          selectedColumnsEach._2.head.selectColumnName.orNull,
          "seq" ->selectedColumnsEach._2.head.selectedColumnSeq,
          "formatting" -> selectedColumnsEach._2
            )
         )
       )
     )
   )
 )

我使用.toSeq将结果读入一个Seq[Map[String,Any]],然后使用toJson将其转换成一个Json

val s = new SaveTemplate getReportsWithDashboardId(dashboardId)
val result : Seq[Map[String,Any]] =  s.groupBy(_.dashboardId)
  .map(
    each => Map(
      "dashboardId" -> each._1,
      "cubeId" -> each._2.head.cubeid,
      "dashboardName" -> each._2.head.dashboardName,
      "reports" -> each._2.groupBy(_.reportId).map(
        reportEach => Map(
          "reportId" -> reportEach._1,
          "reportName" -> (reportEach._2.find(_.reportName != null) match {
          case Some(reportNameData) => reportNameData.reportName
          case None => null
        }),
          "selectedColumns" -> reportEach._2.groupBy(_.selectedColumnid).map(
            selectedColumnsEach => Map(
              "selectedColumnId" -> selectedColumnsEach._1,
              "columnName" -> selectedColumnsEach._2.head.selectColumnName.orNull,
              "seq" ->selectedColumnsEach._2.head.selectedColumnSeq,
              "formatting" -> Map(
              "formatId" -> (selectedColumnsEach._2.find(_.formatId != null) match {
                case Some(reportNameData) => reportNameData.formatId
                case None => null
              }),
              "formattingId" -> (selectedColumnsEach._2.find(_.formattingid != null) 
              match {
                case Some(reportNameData) => reportNameData.formattingid
                case None => null
              }),
              "type" -> (selectedColumnsEach._2.find(_.formattingType != null) match 
              {
                case Some(reportNameData) => reportNameData.formattingType
                case None => null
              })
            )
           )
          )
        )
      )
    )
  ).toSeq
val k = toJson(result)
Ok(k)

不能将
Map[String,Any]
转换为Json,但可以将
Map[String,String]
Map[String,JsValue]
转换为Json

在您的情况下,您可以通过以下方法将每个映射值转换为
JsValue

Map(
  "dashboardId" -> Json.toJson(each._1),
  "cubeId" -> Json.toJson(each._2.head.cubeid),
  "dashboardName" -> Json.toJson(each._2.head.dashboardName),
  "reports" -> Json.toJson(each._2.groupBy(_.reportId).map(
    reportEach => Map(
      "reportId" -> Json.toJson(reportEach._1),
      "reportName" -> (reportEach._2.find(_.reportName != null) match {
      case Some(reportNameData) => Json.toJson(reportNameData.reportName)
      case None => JsNull
    })),
  ...
)

您尝试了什么?找到了一个解决方案
Any
是否有问题,特别是如果您想使用类型类,这需要类型来解析正确的实例。