Arrays Scala play api for JSON-从字符串化的JSON获取某个case类的数组?
从我们的代码中,我们调用了一些服务,并得到了字符串化的JSON。字符串化的JSON是一个“SomeItem”数组,其中只有四个字段——3个long和1个String 例: 我一直在使用PlayAPI通过隐式写/读来读取值。但我很难让它为阵列工作 例如,我尝试解析响应中的值,然后将其转换为SomeItem case类数组,但失败了:Arrays Scala play api for JSON-从字符串化的JSON获取某个case类的数组?,arrays,json,scala,playscalajs,Arrays,Json,Scala,Playscalajs,从我们的代码中,我们调用了一些服务,并得到了字符串化的JSON。字符串化的JSON是一个“SomeItem”数组,其中只有四个字段——3个long和1个String 例: 我一直在使用PlayAPI通过隐式写/读来读取值。但我很难让它为阵列工作 例如,我尝试解析响应中的值,然后将其转换为SomeItem case类数组,但失败了: val sanityCheckValue: JsValue: Json.parse(response.body) val Array[SomeItem] = Jso
val sanityCheckValue: JsValue: Json.parse(response.body)
val Array[SomeItem] = Json.fromJson(sanityCheckValue)
我有
implicit val someItemReads = Json.reads[SomeItem]
但它看起来好像不起作用。我尝试设置一个Json.reads[Array[SomeItem]],但没有成功
这样行吗?关于如何让它工作,有什么建议吗
import play.api.libs.json._
case class SomeItem(id: Long, count: Long, someOtherCount: Long, someString: String)
object SomeItem {
implicit val format = Json.format[SomeItem]
}
object PlayJson {
def main(args: Array[String]): Unit = {
val strJson =
"""
|[
| {"id":33,"count":40000,"someOtherCount":0,"someString":"stuffHere"},
| {"id":35,"count":23000,"someOtherCount":0,"someString":"blah"}
|]
""".stripMargin
val listOfSomeItems: Array[SomeItem] = Json.parse(strJson).as[Array[SomeItem]]
listOfSomeItems.foreach(println)
}
}
import play.api.libs.json._
case class SomeItem(id: Long, count: Long, someOtherCount: Long, someString: String)
object SomeItem {
implicit val format = Json.format[SomeItem]
}
object PlayJson {
def main(args: Array[String]): Unit = {
val strJson =
"""
|[
| {"id":33,"count":40000,"someOtherCount":0,"someString":"stuffHere"},
| {"id":35,"count":23000,"someOtherCount":0,"someString":"blah"}
|]
""".stripMargin
val listOfSomeItems: Array[SomeItem] = Json.parse(strJson).as[Array[SomeItem]]
listOfSomeItems.foreach(println)
}
}