我如何在Scala中使用Gson来序列化List? [英] How can I use Gson in Scala to serialize a List?

查看:512
本文介绍了我如何在Scala中使用Gson来序列化List?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经希望将Scala和Gson一起使用。它似乎主要工作,但是当我做这样的事情时,它将列表视为一个对象,而不是一个数组:

  case class MyType(val x:String,val y:List [SomeOtherType]){
def toJson()= new Gson()。toJson(this)
}
 <$ c 

$ c $ {
x:whatever,
y:{

}
}

通常情况下,Gson会将列表转换为数组。我确信这都是因为Gson不知道Scala的集合类,但是有什么想法可以做到这一点?或者使用Scala本地JSON库的其他建议?

解决方案

您可以尝试解除json,它是本地scala lib:http://www.assembla.com/spaces/liftweb/wiki/JSON_Support


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 class MyType (val x:String, val y:List[SomeOtherType]) {
    def toJson() = new Gson().toJson(this)
}

And my JSON turns out something like this:

{
    "x":"whatever",
    "y": {

    }
}

Normally Gson converts lists to arrays. I'm sure this is all because Gson doesn't know about Scala's collection classes, but any ideas on what I can do to make this work? Or other suggestions using Scala-native JSON libraries?

解决方案

You may try lift json, it's native scala lib: http://www.assembla.com/spaces/liftweb/wiki/JSON_Support

这篇关于我如何在Scala中使用Gson来序列化List?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆