如何在 Scala 中解析带有空值的 json 列表?
How to parse json list with nulls in scala?
我正在尝试解析 json 对象,里面有一个列表
{
"foo": 12,
"bar": [ 12, null, null, 32 ]
}
在我们的项目中,我们参考了 scala play 文档并尝试声明一个简单的 json 读取解析器,例如 Json.reads[MyObject]
案例 class 看起来像:
case class MyObject(
foo: Int,
bar: List[Option[Int]]
)
但是编译器抱怨它:
Error:(93, 42) No instance of play.api.libs.json.Reads is available for scala.collection.immutable.List[scala.Option[scala.Int]] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
implicit val readMyObject = Json.reads[MyObject]
有人知道解决方法吗?
https://www.playframework.com/documentation/2.6.x/ScalaJsonCombinators
将optionReads
方法添加到对象伴侣。
val str = """{
"foo": 12,
"bar": [ 12, null, null, 32 ]
}"""
case class MyObject(foo: Int, bar: List[Option[Int]])
object MyObject {
implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
implicit val reads = Json.reads[MyObject]
}
val json = Json.parse(str)
val obj = Json.fromJson[MyObject](json)
println(obj)
// JsSuccess(MyObject(12,List(Some(12), None, None, Some(32))),)
我不确定为什么
implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
导致编译错误:
Error:(11, 73) type mismatch; found : play.api.libs.json.JsValue => play.api.libs.json.JsResult[Option[T]] required: play.api.libs.json.Reads[Option[T]] implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
但我已经通过添加
解决了它
implicit def optionFormat[T: Format]: Format[Option[T]] =
new Format[Option[T]] {
override def reads(json: JsValue): JsResult[Option[T]] =
json.validateOpt[T]
}
来自 post
我正在尝试解析 json 对象,里面有一个列表
{
"foo": 12,
"bar": [ 12, null, null, 32 ]
}
在我们的项目中,我们参考了 scala play 文档并尝试声明一个简单的 json 读取解析器,例如 Json.reads[MyObject]
案例 class 看起来像:
case class MyObject(
foo: Int,
bar: List[Option[Int]]
)
但是编译器抱怨它:
Error:(93, 42) No instance of play.api.libs.json.Reads is available for scala.collection.immutable.List[scala.Option[scala.Int]] in the implicit scope (Hint: if declared in the same file, make sure it's declared before)
implicit val readMyObject = Json.reads[MyObject]
有人知道解决方法吗?
https://www.playframework.com/documentation/2.6.x/ScalaJsonCombinators
将optionReads
方法添加到对象伴侣。
val str = """{
"foo": 12,
"bar": [ 12, null, null, 32 ]
}"""
case class MyObject(foo: Int, bar: List[Option[Int]])
object MyObject {
implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
implicit val reads = Json.reads[MyObject]
}
val json = Json.parse(str)
val obj = Json.fromJson[MyObject](json)
println(obj)
// JsSuccess(MyObject(12,List(Some(12), None, None, Some(32))),)
我不确定为什么
implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
导致编译错误:
Error:(11, 73) type mismatch; found : play.api.libs.json.JsValue => play.api.libs.json.JsResult[Option[T]] required: play.api.libs.json.Reads[Option[T]] implicit def optionReads[T:Reads]: Reads[Option[T]] = (json: JsValue) => json.validateOpt[T]
但我已经通过添加
解决了它 implicit def optionFormat[T: Format]: Format[Option[T]] =
new Format[Option[T]] {
override def reads(json: JsValue): JsResult[Option[T]] =
json.validateOpt[T]
}
来自 post