我如何在Scala中使用Gson来序列化List? [英] How can I use Gson in Scala to serialize a List?
问题描述
case class MyType(val x:String,val y:List [SomeOtherType]){
def toJson()= new Gson()。toJson(this)
}
$ c $ <$ 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屋!