我使用json4s native
,像这样的json字符串
val myjson = """
{
"normative":"C",
"prefixType":{
"cod":["smallint", "int", "varchar(5)"],
"des":["varchar", "string"],
"fec":["timestamp"],
"hms":["timestamp"],
"tim":["timestamp"],
"imp":["decimal","Float", "Double"]
},
"fixcolname":{
"aud_usuario":"varchar(8)",
"aud_fec":"timestamp",
"aud_tim":"timestamp"
},
"symSep":"_",
"maxLength":26
}"""
案例类
case class colVerify(prefixType: Map[String, Array[String]], fixcolname: Map[String, String], symSep: String, maxLength: Int)
我想从json String
中提取它val t = parse(myjson)
implicit val formats = DefaultFormats
val myvfy = t.extract[colVerify]
然后出现了这样的错误
Exception in thread "main" org.json4s.package$MappingException: Parsed JSON values do not match with class constructor
args=Map(des -> [Ljava.lang.String;@d7b1517, fec -> [Ljava.lang.String;@16c0663d, tim -> [Ljava.lang.String;@23223dd8, hms -> [Ljava.lang.String;@4ec6a292, imp -> [Ljava.lang.String;@1b40d5f0, cod -> [Ljava.lang.String;@ea4a92b),Map(aud_usuario -> varchar(8), aud_fec -> timestamp, aud_tim -> timestamp),_,26
arg types=scala.collection.immutable.HashMap$HashTrieMap,scala.collection.immutable.Map$Map3,java.lang.String,java.lang.Integer
constructor=public colVerify(scala.collection.mutable.Map,scala.collection.mutable.Map,java.lang.String,int)
好像它的Map类型有问题,但我怎么能隐式转换呢?
答案 0 :(得分:1)
问题是您的案例类中的地图是可变地图,这是故意还是您意外地import collection.mutable.Map
?
如果确实想要可变地图,您可以按照此处所述实现自定义序列化程序:https://github.com/json4s/json4s#serializing-non-supported-types
我的第一个想法是在案例类中添加另一个带有不可变映射的构造函数似乎无法可靠地工作。