问题:“提取”在 Android (Scala) 应用程序中使用 lift-json 的 JSON

发布于 2024-11-17 20:08:53 字数 3053 浏览 0 评论 0 原文

我想使用 lift-json 库反序列化 JSON 字符串。在我的 Android 应用程序中,我使用 Scala 2.9.0 和 lift-json_2.9.0_2.4-M1。

我从 lift-json 自述文件中获取了一个简单的示例,但每次我尝试从 JSON 字符串中提取值时,调用 Activity 时都会收到 net.liftweb.json.MappingException 。似乎没有传递给“extract”的参数。

这是我的活动:

import _root_.android.app.Activity
import _root_.android.os.Bundle
import net.liftweb.json._

class JsonTest extends Activity {

  override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.mainactivity)

    implicit val formats = DefaultFormats
    case class NumList(numbers: List[Int])
    val json = parse(""" { "numbers" : [1, 2, 3, 4] } """)
    json.extract[NumList]
  }
}

这是我得到的例外:

06-29 12:09:31.548: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhs.spirit/de.fhs.spirit.JsonTest}: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
    args=
    arg types=
    constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
    args=
    arg types=
    constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
    at net.liftweb.json.Meta$.fail(Meta.scala:185)
    at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:257)
    at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:280)
    at net.liftweb.json.Extraction$.build$1(Extraction.scala:298)
    at net.liftweb.json.Extraction$.extract0(Extraction.scala:345)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$$extract0(Extraction.scala:194)
    at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:290)
    at de.fhs.spirit.JsonTest.onCreate(JsonTest.scala:16)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

如果您知道如何解决我的问题,那就太好了。 谢谢你!

问候, 伊拉兹

I want to deserialize a JSON-String using the lift-json library. In my Android application I'm using Scala 2.9.0 and lift-json_2.9.0_2.4-M1.

I took a simple example from lift-json readme, but everytime I try to extract values from the JSON-String I get a net.liftweb.json.MappingException when calling the Activity. It seems there are no args delivered to "extract".

Here is my Activity:

import _root_.android.app.Activity
import _root_.android.os.Bundle
import net.liftweb.json._

class JsonTest extends Activity {

  override def onCreate(savedInstanceState: Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.mainactivity)

    implicit val formats = DefaultFormats
    case class NumList(numbers: List[Int])
    val json = parse(""" { "numbers" : [1, 2, 3, 4] } """)
    json.extract[NumList]
  }
}

and here the exception I get:

06-29 12:09:31.548: ERROR/AndroidRuntime(405): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.fhs.spirit/de.fhs.spirit.JsonTest}: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
    args=
    arg types=
    constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:123)
    at android.app.ActivityThread.main(ActivityThread.java:4627)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: net.liftweb.json.MappingException: Parsed JSON values do not match with class constructor
    args=
    arg types=
    constructor=public de.fhs.spirit.JsonTest$NumList$2(de.fhs.spirit.JsonTest,scala.collection.immutable.List)
    at net.liftweb.json.Meta$.fail(Meta.scala:185)
    at net.liftweb.json.Extraction$.instantiate$1(Extraction.scala:257)
    at net.liftweb.json.Extraction$.newInstance$1(Extraction.scala:280)
    at net.liftweb.json.Extraction$.build$1(Extraction.scala:298)
    at net.liftweb.json.Extraction$.extract0(Extraction.scala:345)
    at net.liftweb.json.Extraction$.net$liftweb$json$Extraction$extract0(Extraction.scala:194)
    at net.liftweb.json.Extraction$.extract(Extraction.scala:42)
    at net.liftweb.json.JsonAST$JValue.extract(JsonAST.scala:290)
    at de.fhs.spirit.JsonTest.onCreate(JsonTest.scala:16)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    ... 11 more

Would be great if you have an idea how to solve my problem.
Thank you!

Greetings,
Illaz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴情 2024-11-24 20:08:53

我怀疑 Lift 在这种模式下使用了反射。它可能在 Android 中根本不起作用,或者可能要求某些已删除的内容被删除。我建议您尝试使用其他一些替代方案。

另请参阅此问题 -- 我的怀疑来自于它在 REPL 上不起作用的评论。并不是说 REPL 没有反射,而是它的包结构可能会让 Lift 库感到困惑。

I suspect Lift uses reflection in this mode. It might not work at all in Android, or it may require that some stuff that got removed not be removed. I suggest you just try to use some of other alternatives instead.

See also this question -- my suspicion comes from the remark that it doesn't work on REPL. Not that REPL doesn't have reflection, but its package structure might be confusing to the Lift library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文