【问题标题】:Parsing JSON with Scala lift使用 Scala lift 解析 JSON
【发布时间】:2011-11-09 21:08:59
【问题描述】:

我正在尝试解析属性名称(点)中带有特殊字符的 json 字符串。

这就是我正在尝试的:

//Json parser objects
case class SolrDoc(`rdf.about`:String, `dc.title`:List[String],
                   `dc.creator`:List[String], `dc.dateCopyrighted`:List[Int],
                   `dc.publisher`:List[String], `dc.type` :String)
case class SolrResponse(numFound:String, start:String, docs: List[SolrDoc])

val req = url("http://localhost:8983/solr/select") <<? Map("q" -> q)
var search_result = http(req ># { json => (json \ "response") })

var response = search_result.extract[SolrResponse]

即使我的 json 字符串包含所有字段的值,这也是我得到的错误:

Message: net.liftweb.json.MappingException: No usable value for docs
No usable value for rdf$u002Eabout
Did not find value which can be converted into java.lang.String

我怀疑这与名称上的点有关,但到目前为止我还没有设法使它起作用。

谢谢!

这些是我的 LiftProject.scala 文件的摘录:

"net.databinder" % "dispatch-http_2.8.1" % "0.8.6",
"net.databinder" % "dispatch-http-json_2.8.1" % "0.8.6",
"net.databinder" % "dispatch-lift-json_2.8.1" % "0.8.6"

【问题讨论】:

  • Databinder pulls 和旧版本的 lift json,在 lift 邮件列表上有一个关于它的线程,我现在找不到它,但会在今天晚些时候发布链接。

标签: json scala lift


【解决方案1】:

名称中的点应该不是问题。这是与 lift-json-2.4-M4

scala> val json = """ {"first.name":"joe"} """
scala> parse(json).extract[Person]
res0: Person = Person(joe)

在哪里

case class Person(`first.name`: String)

【讨论】:

  • 会不会和dispatch.liftjson.Js._有关?我正在使用调度库 (http(req ># { json => (json \ "response") })) 调用 solr,然后处理我之前发布的结果。
  • 我不确定,但我认为 dispatch.liftjson.Js 应该不是问题。在某些旧版本的 Lift JSON 中存在与符号字段名称相关的错误。我的猜测是您使用的是旧版本?
  • 这是我正在使用的版本:“net.databinder”%“dispatch-http_2.8.1”%“0.8.6”,“net.databinder”%“dispatch-http-json_2. 8.1" % "0.8.6", "net.databinder" % "dispatch-lift-json_2.8.1" % "0.8.6" 我认为是最新的。
  • 刚刚编辑了我的原始问题以包含最新信息。