简体   繁体   English

如何在Scala / Play中将JSON转换为简单的旧数据(Map,List,String,Int等)

[英]How do I turn JSON into plain old data (Map, List, String, Int etc.) in Scala/Play

I have a JsValue corresponding to the following JSON object: 我有一个与以下JSON对象相对应的JsValue

{"foo": [{"id": 1, value: "bar"}, {"id": 2, "value": "baz"}, ...]}

Is there some easy way to turn this into a Map(String, List(Map(String, Either(String, Int)))) or some other structure of regular ol' data? 是否有一些简单的方法可以将其转换为Map(String, List(Map(String, Either(String, Int))))或其他常规数据的结构?

I want to query the JSON for whether <anonymous outermost value>["foo"] contains {"id": 42, "value": "Towel"} . 我想查询JSON是否<anonymous outermost value>["foo"]包含{"id": 42, "value": "Towel"} This would be easy for me if I could convert the JsValue object to plain old collections*. 如果我可以将JsValue对象转换为普通的旧集合,这对我来说将很容易。 Is there some other straightforward way to do so on JsValue objects? 在JsValue对象上还有其他简单的方法吗?

(*) I think it'd be something like anonymous_outermost_value("foo") contains Map("id" -> Left(42), "value" -> Right("Towel")) . (*)我认为这将是类似于anonymous_outermost_value("foo") contains Map("id" -> Left(42), "value" -> Right("Towel"))

(In this particular application, I would be OK with converting all the IDs to strings and turning the Either(String, Int) into just String , if that makes stuff easier.) (在这个特定的应用程序中,我可以将所有ID都转换为字符串,然后将Either(String, Int)转换为String ,如果这样可以使事情变得更容易。)

Work with Map , List etc is not easier than work with Playframework Json. 使用MapList等并不比使用Playframework Json容易。 If you want use the usual way to work with data, JSON basics helps you. 如果您想使用通常的方式处理数据, JSON基础知识将为您提供帮助。

You must implement converters Json <-> Plain objects with Json Reads[T] and Writes[T] 您必须使用Json的Reads[T]Writes[T]实现转换器Json <->普通对象。

For example Json object {"id": 1, value: "bar"} represented with case class A(id: Long, value: String) and {"foo": [{"id": 1, value: "bar"}.. represented with case class B(foo: List[A]) . 例如{"id": 1, value: "bar"}case class A(id: Long, value: String){"foo": [{"id": 1, value: "bar"}..表示的Json对象{"id": 1, value: "bar"} {"foo": [{"id": 1, value: "bar"}..case class B(foo: List[A]) The simplest way for implement converter Json <-> Plain objects is Json.format[T] macros. 实现转换器Json <->普通对象的最简单方法是Json.format [T]宏。

implicit val aFormat = Json.format[A]
implicit val bFormat = Json.format[A]

Then you may use Json.toJson[T](o: T) <-> Json.fromJson[T](json: JsValue) for converting T <-> JsValue 然后,您可以使用Json.toJson[T](o: T) <-> Json.fromJson[T](json: JsValue)转换T <-> JsValue

You could convert the JsValue to a JsObject (which is simply a wrapper around a Map[String, JsValue] ), extracting the foo property as a JsArray (which is a wrapper around a Seq[JsValue] ) and so on. 您可以将JsValue转换为JsObject (它只是Map[String, JsValue]的包装),将foo属性提取为JsArray (它是Seq[JsValue]的包装),依此类推。 But I think it's really easier to simply use the Json functions: 但是我认为简单地使用Json函数确实更容易:

for {
  foo <- (json \ "foo").validate[Seq[JsObject]]
  obj <- foo
  id <- (obj \ "id").validate[Int]
  value <- (obj \ "value").validate[String]
  if id == 42 && value == "Towel"
} {
  // do something
}

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

相关问题 使用go,如何将map [int] T转换为map [string] T以与JSON一起使用? - using go, how do i turn a map[int]T to a map[string]T to use with JSON? 播放scala读取带实例的Map [Int,List [String]] - play scala reads for Map[Int,List[String]] with exeptions 如何使用Play Framework JSON库为Map [Int,Int]创建JSON格式? - How do I create a JSON format for Map[Int, Int] using Play Framework JSON libs? 如何将 json 字符串解析为 scala 中的 Map 列表? - How to parse a json string to a List of Map in scala? 在Scala中,如何为Map [IndexedSeq [String],Int]编写一个高效的json格式化程序? - In Scala, how to write an efficient json formatter for Map[IndexedSeq[String], Int]? 在空手道DSL中,如何将替换文本用于其他数据类型,例如int,float,Big等? - In Karate DSL, how can I use the replace text for other data types such as int, float, Big, etc.? Map的Scala Play Json格式[语言环境,字符串] - Scala Play Json Format for Map[Locale, String] 如何将json字符串转换为datatable? - How do I turn the json string to datatable? 如何将字符串转换为JSON对象? - How do I turn a string into a JSON object? 如何将由String和Seq [String]值组成的Scala Map转换为JSON? - How do I convert a Scala Map consisting of String and Seq[String] values to JSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM