Вопрос

I'm reading data from a KV store (Redis) in this case. The data returned is in the following format.

 { "key1":"value1", "key2":"value2", "key3":"value3" ...} 

Key is String and value is Int. I want to convert it into a Map[String,Int]

I looked at the json4s JSON API and my current code looks like the following. Is there a better/easier/cleaner way of doing this?

  //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")
  }
Это было полезно?

Решение 2

Your Json4s solution looks fine. Alternatively you can use mapField to transform the fields of a JObject and after that extract value of type Map[String, Int].

val json1 = parse(
  """
    |{
    |  "key1": "1024",
    |  "key2": "2048",
    |  "key3": "4096"
    |}
  """.stripMargin)

val json2 = json1.mapField {
  case (key, JString(value)) => (key, JInt(value.toInt))
  case x => x
}

val res = json2.extract[Map[String, Int]]

println(res)
// Map(key1 -> 1024, key2 -> 2048, key3 -> 4096)

Другие советы

This looks to me like the simplest way to do it:

val map = parse("""{"a":"1","b":"2","c":"3"}""")
  .children
  .collect { case JField(k, JString(v)) => (k, v.toInt) }
  .toMap

Not knowing json4s, and unfortunately you ommited the types, but guessing that jsonrows is probably something like a List[(String, String)] you could do List(("key1" -> "1"),("key2" -> "2")).map { case (k, v) => (k, v.toInt)}.toMap

BTW, if you say need to do something with this data now in your onComplete - that could only be a side effecting operation. Better map over the future until your processing is complete.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top