Scala:找不到任何类型的Json序列化程序

Scala:找不到任何类型的Json序列化程序,json,scala,playframework,Json,Scala,Playframework,我有以下代码。以下是vesselFlagCountryDF的模式 root |-- flagcountry: string (nullable = true) |-- max(count): long (nullable = true) |-- min(count): long (nullable = true) |-- avg(count): double (nullable = true) |-- stddev_samp(count,0,0): double (nullable =

我有以下代码。以下是vesselFlagCountryDF的模式

root
 |-- flagcountry: string (nullable = true)
 |-- max(count): long (nullable = true)
 |-- min(count): long (nullable = true)
 |-- avg(count): double (nullable = true)
 |-- stddev_samp(count,0,0): double (nullable = false)
下面是一些示例行:

+--------------------+----------+----------+----------+----------------------+
|         flagcountry|max(count)|min(count)|avg(count)|stddev_samp(count,0,0)|
+--------------------+----------+----------+----------+----------------------+
|              Cyprus|        65|        46|      55.0|      9.40744386111339|
|          Luxembourg|         3|         1|       2.5|    0.9999999999999999|
|                Niue|         5|         3|       4.4|    0.8944271909999159|
|           Palestine|         2|         1|      1.25|   0.49999999999999994|
|              Norway|        30|        18|      23.4|     5.683308895353129|
|            Mongolia|        21|        15|      17.6|     2.302172886644268|
|            Dominica|         1|         1|       1.0|                   0.0|
|British Virgin Is...|         1|         1|       1.0|                   NaN|
+--------------------+----------+----------+----------+----------------------+
现在,stddev(“count”)可以是双精度或Nan

import play.api.libs.json.{JsValue, Json}

  val vesselFlagCountryDF =
    vtype.groupBy("flagcountry").agg(max("count"), min("count"), avg("count"), 
                  stddev("count"))

  vesselFlagCountryDF.collect().foreach(row => {
    val flagCountry = row.getString(row.fieldIndex("flagcountry"))
    val upper: Long = row.getLong(row.fieldIndex("max(count)"))
    val lower: Long = row.getLong(row.fieldIndex("min(count)"))
    val mean: Double = row.getDouble(row.fieldIndex("avg(count)"))
    val stdDevWrapper: Any = row.get(row.fieldIndex("stddev_samp(count,0,0)"))
    val stdDev = stdDevWrapper match {
      case d: Double => d
      case _  => "NaN"
    }

    val json: JsValue = Json.obj(
      "type" -> "statistics",
      "name" -> "vesselCountByFlagCountry",
      "flagCountry" -> flagCountry,
      "timeInterval" -> Json.obj("startTime" -> startTime, "endTime" -> endTime),
      "upper" -> upper,
      "lower" -> lower,
      "mean" -> mean,
      "stdDev" -> stdDev
    )
在这一行:

      "stdDev" -> stdDev
我得到以下错误:

No Json serializer found for type Any. Try to implement an implicit Writes or 
Format for this type. 
[error]           "stdDev" -> stdDev

处理此错误的最佳方法是什么?

以下术语只能推断为
Any
,因为没有父类型来统一
Double
String
,这是不推荐的

val stdDev = stdDevWrapper match {
  case d: Double => d
  case _  => "NaN"
}
另一方面,JSON序列化仅适用于类型化值,而不是
Any

stdDev
可以根据具体情况进行重构,直接写入正确的JSON值

val stdDev: JsValue = stdDevWrapper match {
  case d: Double => Json.toJson(d)
  case _  => Json.toJson("NaN")
}

以下术语只能推断为
Any
,因为没有父类型来统一
Double
String
,这是不推荐的

val stdDev = stdDevWrapper match {
  case d: Double => d
  case _  => "NaN"
}
另一方面,JSON序列化仅适用于类型化值,而不是
Any

stdDev
可以根据具体情况进行重构,直接写入正确的JSON值

val stdDev: JsValue = stdDevWrapper match {
  case d: Double => Json.toJson(d)
  case _  => Json.toJson("NaN")
}