简体   繁体   English

在scala中将JSON对象转换为特定键的值列表(播放)

[英]Converting a JSON object to a value list of a particular key in scala (play)

I have a Json object, which is an output of Json.parse, which is of format 我有一个Json对象,它是Json.parse的输出,其格式是

{ "main_data" : [ {"a" : 1, "b" :2} , {"a" : 3, "b" : 4}, {"a" : 5, "b" : 6}]}. {“ main_data”:[{“ a”:1,“ b”:2},{“ a”:3,“ b”:4},{“ a”:5,“ b”:6}]}。

I want to create a list out of this Json object with the values of all the "a" key. 我想用所有“ a”键的值从该Json对象创建一个列表。 So in the above example it will be [1,3,5]. 因此,在上面的示例中,它将为[1,3,5]。 As I am new to functional programming the first thing came to my mind was to write a For loop and traverse through the Json object to get the list. 当我刚接触函数式编程时,我想到的第一件事是编写一个For循环并遍历Json对象以获取列表。

But I was wondering is there a Functional/Scala way to do the above, using Map or flatMap ? 但是我想知道是否有使用Map或flatMap的Functional / Scala方法来完成上述操作?

import play.api.libs.json._

val parsed = Json.parse("""{"main_data":[{"a":1,"b":2},{"a":3,"b":4},{"a":5,"b":6}]}""")
val keys   = parsed \\ "a"
val result = keys.flatMap(_.asOpt[Int]) // List(1, 3, 5)

You can use something like this 你可以用这样的东西

val json = Json.parse(yourJsonString)
val aKeys = json \\ "a"
// aKeys: Seq[play.api.libs.json.JsValue] = List(1, 3, 5)
// If you need integers instead of JsValues, just use map
val integersList = aKeys.map(x => x.as[Int])

There are plenty of choices. 有很多选择。 Let's say you have: 假设您有:

import play.api.libs.json._

val txt =
  """
    |{ "main_data" : [ {"a" : 1, "b" :2} , {"a" : 3, "b" : 4}, {"a" : 5, "b" : 6}]}
  """.stripMargin


val json = Json.parse(txt)

First approach if you are interested only in a : 如果您只对感兴趣,则a

(json \ "main_data")
  .as[List[Map[String, Int]]]
  .flatten
  .foldLeft(List[Int]()){
    case (acc, ("a", i)) => acc :+ i
    case (acc, _) => acc
  }

The second more general: 第二个更一般:

(json \ "main_data")
  .as[List[Map[String, Int]]]
  .flatten
  .groupBy(_._1)
  .map {
    case (k, list) => k -> list.map(_._2)
  }
  .get("a")

And the result is: 结果是:

res0: Option[List[Int]] = Some(List(1, 3, 5))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Play for Scala中将异构列表转换为Json和从Json转换 - Converting a Heterogeneous List to and from Json in Play for Scala 如何构建一个 json object 给定 Scala 中的字段列表(键 -> 值) - How to build a json object given a list of fields (key -> value) in Scala 使用Play JSON将嵌套的Scala对象转换为JSON字符串 - Converting a nested scala object into a JSON string using Play JSON 将“递归”对象转换为JSON(带有Scala的Play Framework 2.4) - Converting “recursive” object to JSON (Play Framework 2.4 with Scala) 当 json 中的一个键包含值列表时,使用 Retrofit 将 JSON 键值对转换为 Java 对象 - Converting JSON key - value pair into Java object using Retrofit when one of the key in json contains a list of values 为JSON中的所有对象获取特定的键值 - Getting a particular key value for all object in JSON 将键值对的字符串转换为JSON字符串的惯用方式(Scala) - Idiomatic way of converting string of key value pairs to a JSON string (Scala) 使用Play API将Scala对象分配给Json - Scala object to Json with Play API 玩! Scala JSON对象处理 - Play! Scala JSON object handling 使用Scala播放2.3,将JSON数组转换为Sequence - Play 2.3 with Scala, converting JSON Array to Sequence
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM