2

I have the following json

   {  
       "op":{  
          "samples":{  
             "ep_mem_high_wat":[  0,0,0,0,0,0,0],
             "ep_mem_low_wat":[  0,0,0,0,0,0,0]
          },
          "samplesCount":60,
          "isPersistent":true,
          "lastTStamp":1415619627689,
          "interval":1000
       },
       "hot_keys":[  
          {  
             "name":"counter::F03E91E2A4B9C25F",
             "ops":0.11010372549516878
          }
       ]
    }

I would like to parse "samples" property of this Json as Map[String,List[Double]] as

Map[String,List[Double]]("ep_mem_high_wat" -> [ 0,0,0,0,0,0,0],"ep_mem_low_wat" -> [0,0,0,0,0,0,0])

For this purpose I perform the following: I create my case classes

  case class Samples(properties:Map[String,List[Double]])
  case class Op(samples:Samples,samplesCount:Int,isPersistent:Boolean,lastTStamp:Long,interval:Int)
  case class Key(name:String,ops:Double)
  case class BucketStatisticResponse(op:Op,hot_keys:List[Key])

and then I create custom Json protocol

 object BucketStatisticJsonProtocol extends DefaultJsonProtocol {

    implicit object SamplesJsonFormat extends RootJsonFormat[Samples] {

      def write(obj: Samples) = obj.toJson
      def read(value: JsValue) =  {
         val props = value.asJsObject.fields
          Samples(value.convertTo[Map[String,List[Double]]])
      }
    }
    implicit val keyFormat = jsonFormat2(Key)
    implicit val opFormat = jsonFormat5(Op)
    implicit val bucketStatisticFormat= jsonFormat2(BucketStatisticResponse)
  }

And then I'm trying to parse json

 import BucketStatisticJsonProtocol._
 val res =  json.toJson.convertTo[BucketStatisticResponse]

As the result I'm gettign the folloing exception:

Exception in thread "main" spray.json.DeserializationException: Object expected in field 'op'
    at spray.json.package$.deserializationError(package.scala:23)
    at spray.json.ProductFormats$class.fromField(ProductFormats.scala:54)
    at high.availability.poc.DynamicJson$BucketStatisticJsonProtocol$.fromField(DynamicJson.scala:23)

What am I doing wrong?

4

1 回答 1

3

查看错误消息:它抱怨它在 AST 中为该op字段找到的值类型。因此,您实际解析的文档可能与您上面的示例不匹配。

除此之外,您的代码对我来说看起来不错。出于个人喜好,我通常不会扩展DefaultJsonProtocol;我将我的 JSON 格式放在包对象中,并import DefaultJsonProtocol._根据需要处理任何简单的转换。

于 2014-11-17T20:35:24.017 回答