Spray JSON - 仅解析部分JSON响应

时间:2014-11-10 16:00:20

标签: scala spray spray-json spray-client

我从服务器中得到以下Json响应,其中samples对象包含

{  
   "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类及其隐式解析器:

 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))

你能帮忙吗?

1 个答案:

答案 0 :(得分:2)

错误消息说喷-json期待String但是得到了别的,似乎你需要在Op类中将“lastTStamp”定义为Long,如下所示:

case class Op(samples:Samples, lastTStamp:Long, isPersistent:Boolean)