4

代码片段在REPL

scala> import com.codahale.jerkson.Json._
scala> val t = (1, 3.14, "Fred")
scala> generate(t)
res5: String = {"_1":1,"_2":3.14,"_3":"Fred"}

在输出中,我想将标签分配给属性而不是_1, _2, _3。我该怎么做呢?

4

1 回答 1

2

使用 acase class而不是元组:

case class Named(myInt: Int, thisDouble: Double, desc: String)
generate(Named(1, 3.14, "Fred"))

给出:

{"myInt": 1.0,"thisDouble":3.14,"desc":"Fred"}
于 2012-05-22T22:13:10.170 回答