0

我对 Scala/Lift 很陌生,遇到了以下问题:

class Tests {
case class JTest(
      thisUrl:String,
      id:Int,
      desc:String,
      results:String,
      isEnabled:Boolean,
      attackerAppliance:String,
      victimAppliance:String,
      lastModified:String)

def displayTest(in: NodeSeq): NodeSeq = {
implicit val formats = DefaultFormats.withHints(
           ShortTypeHints(List(classOf[JTest])))

val content = fromInputStream( url.openStream ).getLines.mkString("\n")

        val json = parse(content)
        val test = json.extract[JTest]
        Helpers.bind("test", in,
          AttrBindParam("thisUrl", test.thisUrl, "href"),
          "id" -> test.id,
          "desc" -> test.desc,
          "results" -> test.results,
          "isEnabled" -> test.isEnabled,
          "attackerAppliance" -> test.attackerAppliance,
          "victimAppliance" -> test.victimAppliance,
          "lastModified" -> test.lastModified)

}
}

我收到以下错误消息:

Message: net.liftweb.json.MappingException: Do not know how to deserialize 'JTest'

示例 JSON:

{"jsonClass":"JTest","thisUrl":"/api/test/1","id":1,"desc":"Vulnerability in Server Service","results":"/api/test/1/results","isEnabled":true,"attackerAppliance":"/api/appliance/2","victimAppliance":"/api/appliance/6","lastModified":"2012-08-08 11:46:29.004"}

我尝试了几件事,但它接缝我卡在这里。任何人都可以指出我正确的方向吗?顺便提一句。我正在使用 scala 2.7.7!

干杯,恩佐

4

1 回答 1

0

如果您jsonClass从 JSON 输入中删除提示,它应该会解析。就像是:

{"thisUrl":"/api/test/1",
"id":1,
"desc":"Vulnerability in Server Service",
"results":"/api/test/1/results",
"isEnabled":true,
"attackerAppliance":"/‌​api/appliance/2",
"victimAppliance":"/api/appliance/6",
"lastModified":"2012-08-08 11:46:29.004"}

否则,您可以尝试[]在输入周围添加,例如:

[{"jsonClass":"JTest",
"thisUrl":"/api/test/1",
"id":1,
"desc":"Vulnerability in Server Service",
"results":"/api/test/1/results",
"isEnabled":true,
"attackerAppliance":"/‌​api/appliance/2",
"victimAppliance":"/api/appliance/6",
"lastModified":"2012-08-08 11:46:29.004"}]

我认为它正在寻找一个集合,因为该jsonClass属性用于帮助确定将当前项目反序列化为哪个子类型。可能有一种方法可以让它与单个元素一起工作,但我不确定那是什么。

我询问 scala 版本的原因是,此时 2.7.7 已经很老了(2.10 是最新的),更新的版本可能会让查找变得更容易一些。

于 2013-03-25T12:59:23.930 回答