1

我从示例对象包含的服务器收到以下 Json 响应

{  
   "op":{  
      "samples":{ 
          "name1" :[0,0,0,0,0,0,0],
          "name2" :[0,0,0,0,0,0,0],
          "name3" :[0,0,0,0,0,0,0],

            //About 100 more names

          "name99" :[1,2,3,4,5,6,7],
          "name100" :[0,0,0,0,0,0,0],

      },
      "samplesCount":60,
      "isPersistent":true,
      "lastTStamp":1415619627689,
      "interval":1000
   },
   "hot_keys":[  
      {  
         "name":"counter::F03E91E2A4B9C25F",
         "ops":0.11010372549516878
      }
        //About 40 objects
   ]
}

我只需要这个结果的一部分。

需要以下属性:

name1, name23, timeStamp and isPersistant  

所以我创建了以下案例类及其隐式解析器:

 case class Samples(name1[Int],name23[Int])
  case class Op(samples:Samples,lastTStamp:String,isPersistent:Boolean)
  case class BucketStatisticResponse(op:Op)

  object BucketStatisticJsonProtocol extends DefaultJsonProtocol {
    implicit val samplesFormat = jsonFormat2(Samples)
    implicit val opFormat = jsonFormat3(Op)
    implicit val bucketStatisticFormat= jsonFormat1(BucketStatisticResponse)
  }

但我收到以下错误:

spray.httpx.PipelineException: MalformedContent(Expected String as JsString, but got 1069547520,Some(spray.json.DeserializationException: Expected String as JsString, but got 1069547520))

你能帮忙吗?

4

1 回答 1

2

错误消息说 spray-json 期待 String 但得到了其他东西,看来您需要在 Op 类中将“lastTStamp”定义为 Long,如下所示:

case class Op(samples:Samples, lastTStamp:Long, isPersistent:Boolean)
于 2014-11-10T16:16:50.893 回答