How can I use Gson in Scala to serialize a List?

后端 未结 5 933
感动是毒
感动是毒 2021-01-04 09:54

I was hoping to use Scala and Gson together. It seems to mostly work, but when I do something like this, it treats the list as an object, not an array:

case          


        
5条回答
  •  别那么骄傲
    2021-01-04 10:24

    You can use a java converter:

    import scala.collection.JavaConverters._
    case class MyType (val x:String, val y:List[SomeOtherType]) {
       def toJson() = new Gson().toJson(this.asJava())
    }
    

提交回复
热议问题