6
我花了最后一天搜索并阅读各种网站和文章,以尝试自己找到此问题的答案,但我没有找到任何有用的信息。我甚至不确定这是否可行。我的问题是,我试图使用lift-json解析和提取Json响应。该响应由4个部分组成,其中前3个部分对于我所做的每种请求的每个响应都是相同的。最后一部分取决于请求的类型,但它总是会成为某种类型的列表。我希望做这样的事情:将lift-json提取为具有上限的案例类
abstract class MyObjects
case class Apple(id: Int, name: String, color: String) extends MyObjects
case class Orange(id: Long, name: String, state: String) extends MyObjects
abstract class MyResponse
case class Fruits[T <: MyObjects](aisle: Int, bin: Int, hasWhat: Option[List[T]])
在哪里,如果我想知道所有的苹果,我会做一个请求为,并取回了苹果的列表的响应。当我尝试提取这个例子:
myJson.extract[Fruits[Apple]]
我得到这个错误:
net.liftweb.json.MappingException: do not know how to get type parameter from T
at net.liftweb.json.Meta$.fail(Meta.scala:128)
at net.liftweb.json.Meta$Reflection$.term$1(Meta.scala:206)
at net.liftweb.json.Meta$Reflection$.typeParameters(Meta.scala:220)
at net.liftweb.json.Meta$.mkContainer$1(Meta.scala:91)
at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:101)
at net.liftweb.json.Meta$.mkContainer$1(Meta.scala:90)
at net.liftweb.json.Meta$.fieldMapping$1(Meta.scala:107)
at net.liftweb.json.Meta$.toArg$1(Meta.scala:117)
at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:83)
at net.liftweb.json.Meta$$anonfun$constructors$1$1$$anonfun$apply$1.apply(Meta.scala:82)
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:...
我使用的升降JSON 2.1 &斯卡拉2.8。我有办法解决这个问题,特别是为每种类型的响应创建一个case类,但是我认为我想要做的更清晰。只是想知道如果 a)这甚至可能吗? b)如果是这样,我做错了什么?
编辑...示例应用程序:
val apples = """{ "aisle" : 1, "bin" : 1,
"hasWhat" : [{ "id" : 4, "name" : "Granny", "color" : "green"},
{ "id" : 4, "name" : "Fuji", "color" : "red"}] }"""
val oranges = """ { "aisle" : 3, "bin" : 2,
"hasWhat" : [{ "id" : 2, "name" : "Navel", "state" : "FL" },
{ "id" : 2, "name" : "Clementine", "state" : "Spain" }]}"""
scala> val aJson = parse(apples)
aJson: net.liftweb.json.JsonAST.JValue = JObject(List(JField(aisle,JInt(1)), JField(bin,JInt(1)), JField(hasWhat,JArray(List(JObject(List(JField(id,JInt(4)), JField(name,JString(Granny)), JField(color,JString(green)))), JObject(List(JField(id,JInt(4)), JField(name,JString(Fuji)), JField(color,JString(red)))))))))
scala> val oJson = parse(oranges)
oJson: net.liftweb.json.JsonAST.JValue = JObject(List(JField(aisle,JInt(3)), JField(bin,JInt(2)), JField(hasWhat,JArray(List(JObject(List(JField(id,JInt(2)), JField(name,JString(Navel)), JField(state,JString(FL)))))))))
scala> val doesntWork = aJson.extract[Fruits]
doesntWork: org.spin.node.gogrid.objects.Fruits = Fruits(1,1,None)
scala> val works = aJson.extract[AFruit]
works: org.spin.node.gogrid.objects.AFruit = AFruit(1,1,Some(List(Apple(4,Granny,green), Apple(4,Fuji,red))))
我想doesntWork要像作品,其中:
case class AFruit(aisle: Int, bin: Int, hasWhat: Option[List[Apple]])
谢谢! -newbie
乔尼你好,谢谢你的输入,是有没有什么地方我可以在不同的配置,以及如何系列化的作品读了?它不太有效。我编辑了我的帖子并用一个例子进行了澄清。我会得到像苹果和橘子那样的字符串,我需要最终得到像作品一样的东西。 – CaffiendFrog 2010-11-03 19:29:36
嗨,最好的资源是https://github.com/lift/lift/tree/master/framework/lift-base/lift-json/和https://github.com上的示例(* Example.scala)/lift/lift/tree/master/framework/lift-base/lift-json/src/test/scala/net/liftweb/json /。在这一点上,你不可能在你的文章工作中做出'没有工作'的情况。这可能是我们可以在未来改善反射代码。 – Joni 2010-11-03 19:36:47
嗨,Joni,我绝对可以生活在像AFruit这样的案例课程中,以支持我需要做的事情,很高兴知道'不工作'的案例不被支持......我现在可以继续完成我的其余任务。 :)。再次感谢,特别感谢这么快的回应,非常感谢。 – CaffiendFrog 2010-11-03 19:43:38