在这种情况下,我正在从 KV 存储(Redis)读取数据。返回的数据格式如下。
{ "key1":"value1", "key2":"value2", "key3":"value3" ...}
键是String
,值是Int
。我想把它转换成Map[String,Int]
我查看了json4s JSON API,我当前的代码如下所示。有没有更好/更容易/更清洁的方法来做到这一点?
//send a async query to Redis to
val queryFuture = redis.zrangebyscore[String](tablename, keymin, keymax )
queryFuture.onComplete {
case Success(datarows) =>
println(s"Got %d rows of type %s for the query successfully".format(datarows.length))
val jsonrows = for { x <- datarows.toList }
yield parse(x)
println("Got json rows %d".format(jsonrows.size))
val mapdata = jsonrows.map(x => x.extract[Map[String,String]]).map( x => x.mapValues(_.toInt))
//need to do something with this data now
case Failure(y) =>
println(s" there was some failure in getting the data from Redis")
}