[英]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. 使用
Map
, List
等并不比使用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.