如何在Play Scala中将地图转换为Json

时间:2018-10-06 06:38:22

标签: json scala playframework

如何将以下Map [String,Any]的Map结构转换为Scala中的Json?我正在使用Play。

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

2 个答案:

答案 0 :(得分:1)

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

对于您来说,可以通过以下操作将每个映射值转换为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
    })),
  ...
)

答案 1 :(得分:0)

我使用.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)