问题描述
给出一个带有嵌套对象的JSON对象,该对象的属性是未知的:
Given an example JSON object with a nested object who's properties are unkown:
{ "Key":"01234",
"eventProperties":{
"unknownProperty1":"value",
"unknownProperty2":"value",
"unknownProperty3":"value"
},
}
我试图将json4s的提取函数与以下案例类一起使用(In Scala):
I have tried to use json4s' extract function with the following case class (In Scala):
case class nestedClass(Key:String, eventProperties:Map[(String,Any)])
导致以下错误:
org.json4s.package$MappingException: Can't find constructor for nestedClass
是否可以在不定义eventProperties的每个可能属性的情况下执行此操作?
Is it possible to do this without defining every possible property of eventProperties?
更新: json4s 3.2.10中存在导致此问题的错误 - 更新到3.2.11并解压缩到Map [String ,任何]工作正常。
Update: there was a bug in json4s 3.2.10 causing this issue - updating to 3.2.11 and extracting to Map[String,Any] works fine.
推荐答案
我不知道你在做什么来获得你发布的例外,但是以下作品(注意地图
而不是列表
):
I'm not sure what you are doing to get the exception you have posted, but the following works (note a Map
instead of List
):
import org.json4s._
import org.json4s.jackson.JsonMethods._
import org.json4s.DefaultFormats
val json = parse("""
{ "key":"01234",
"eventProperties":{
"unknownProperty1":"value",
"unknownProperty2":"value",
"unknownProperty3":"value"
}
}
""")
case class NestedClass(key:String, eventProperties:Map[String,Any])
implicit val formats = DefaultFormats
json.extract[NestedClass]
这篇关于如何使用Json4s在案例类中表示嵌套的JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!