小编JAS*_*itz的帖子

将嵌套列表转换为json会产生多余的数组

我有一些历史数据,我想转换为json.所以这些是列表列表.它们的类型是List [List [Position]],其中Position是一个简单的case类.我写了一个格式化程序来帮助Json.toJson应对.我期待一个外部数组的输出,其中两个内部数组各包含3个对象.我得到的是这个.请注意其他数组包装.

[[[[{"amount":1.0,"minAmount":2.0,"price":3.0,"volume":4.0},
{"amount":5.0,"minAmount":6.0,"price":7.0,"volume":8.0},
{"amount":9.0,"minAmount":10.0,"price":11.0,"volume":12.0}]],
[[{"amount":0.1,"minAmount":0.2,"price":0.3,"volume":0.4},
{"amount":5.0,"minAmount":6.0,"price":7.0,"volume":8.0},
{"amount":9.0,"minAmount":10.0,"price":11.0,"volume":12.0}]]]]
Run Code Online (Sandbox Code Playgroud)

我不知道包装数组的来源.有人可以帮帮我吗?这是我正在使用的包装器的测试:

class ApplicationSpec extends Specification {

  implicit object PositionFormat extends Format[List[List[Position]]] {
    def writes(historyList: List[List[Position]]) : JsValue = {
      Json.arr(historyList.map{
        o => Json.arr(o.map{ p =>
          Json.obj(
            "amount"    -> JsNumber(p.amount),
            "minAmount" -> JsNumber(p.minAmount),
            "price"     -> JsNumber(p.price),
            "volume"    -> JsNumber(p.volume)
          )
        })
      })
    }
    def reads(json: JsValue): JsResult[List[List[Position]]] = ???
  }

  "Application" should {

    "Convert position data to json" in {
      val l1 = ListBuffer(new Position(1.0D,2.0D,3.0D,4.0D),
        new Position(5.0D,6.0D,7.0D,8.0D),
        new Position(9.0D,10.0D,11.0D,12.0D)).toList
      val l2 = …
Run Code Online (Sandbox Code Playgroud)

json scala playframework-2.0

3
推荐指数
1
解决办法
1083
查看次数

标签 统计

json ×1

playframework-2.0 ×1

scala ×1