我有一個List[Any]我想轉換為JsArray.
型別作品串列:
Json.arr(List("1"))
但:
Json.arr(List("1").asInstanceOf[List[Any]])
拋出:
diverging implicit expansion for type play.api.libs.json.Reads[T1]
starting with method oFormatFromReadsAndOWrites in object OFormat
我怎樣才能轉換List[Any]為JsArray?
我試過:
implicit val listAnyFormat: OFormat[List[Any]] = Json.format[List[Any]]
但我被拋出:
No instance of Reads is available for scala.collection.immutable.Nil in the implicit scope
使用 Play 2.8.x 和 Scala 2.11.8
uj5u.com熱心網友回復:
你不能。
至少在沒有定義Format[Any]可以在技術上完成但可能不會涵蓋所有可能情況的情況下。
問題是你為什么首先有一個List[Any]?它在 Scala 世界中沒有多大意義。
如果你能有一個List[Something]whereSomething有一組已知的子型別并且每個子型別都有一個Format.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/397646.html
