下面是我的代码
import scala.collection.immutable.HashMap
import net.liftweb.json.DefaultFormats
object Test {
implicit val formats = DefaultFormats
case class JSONProps(abc: HashMap[String,String])
val json = net.liftweb.json.parse(
"""
{ "abc":
{
"key1": "value1",
"key2": "value2",
"key3": "value3",
"key4": "value4"
}
}
"""
)
def main(args: Array[String]) {
val prop = json.extract[JSONProps]
println(prop.abc)
}
}
当我执行它时,我有以下异常。
Exception in thread "main" net.liftweb.json.MappingException: Parsed
JSON values do not match with class constructor
args=Map(key1 -> value1, key2 -> value2, key3 -> value3, key4 -> value4)
arg types=scala.collection.immutable.Map$Map4
constructor=public
com.abcd.test.Test$JSONProps(scala.collection.immutable.HashMap)
at net.liftweb.json.Meta$.fail(Meta.scala:227)
at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:271)
但是当我再添加一个键作为(key5,value5)时,它没有异常,输出为
Map(key4 -> value4, key5 -> value5, key1 -> value1, key2 -> value2, key3 -> value3)
谁能说出为什么它会这样?