JSON4S未知错误

前端之家收集整理的这篇文章主要介绍了JSON4S未知错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让json4s提取一些东西,但我得到一个“未知的错误

我的代码看起来像:

import org.json4s._
import org.json4s.jackson.JsonMethods._ 
implicit val formats = org.json4s.DefaultFormats

case class Person(name: String,age: Int)
val json = """{"name":"joe","age":15}"""

print(parse(json).extract[Person])

错误路径:

org.json4s.MappingException: unknown error
    at org.json4s.Extraction$.extract(Extraction.scala:50)
    at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
    at Main$$anon$1.<init>(test.scala:8)
    at Main$.main(test.scala:1)
    at Main.main(test.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
    at scala.tools.nsc.ScriptRunner.scala$tools$nsc$ScriptRunner$$runCompiled(ScriptRunner.scala:171)
    at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner$$anonfun$runScript$1.apply(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply$mcZ$sp(ScriptRunner.scala:157)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
    at scala.tools.nsc.ScriptRunner$$anonfun$withCompiledScript$1.apply(ScriptRunner.scala:131)
    at scala.tools.nsc.util.package$.waitingForThreads(package.scala:26)
    at scala.tools.nsc.ScriptRunner.withCompiledScript(ScriptRunner.scala:130)
    at scala.tools.nsc.ScriptRunner.runScript(ScriptRunner.scala:188)
    at scala.tools.nsc.ScriptRunner.runScriptAndCatch(ScriptRunner.scala:201)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:58)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)
    at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.NoSuchFieldException: MODULE$
    at java.lang.Class.getField(Class.java:1520)
    at org.json4s.Meta$$anonfun$mappingOf$1$$anonfun$8.apply(Meta.scala:208)
    at org.json4s.Meta$$anonfun$mappingOf$1.apply(Meta.scala:207)
    at org.json4s.Meta$$anonfun$mappingOf$1.apply(Meta.scala:195)
    at org.json4s.Meta$Memo.memoize(Meta.scala:240)
    at org.json4s.Meta$.mappingOf(Meta.scala:195)
    at org.json4s.Extraction$.mkMapping$1(Extraction.scala:207)
    at org.json4s.Extraction$.org$json4s$Extraction$$extract0(Extraction.scala:214)
    at org.json4s.Extraction$.extract(Extraction.scala:47)
    ... 34 more

我使用jackson解析器2.1.1运行带有json4s 2.9.2:3.1.0的SCALA 2.9.2

在我去“json4s”之前,我直接尝试了杰克逊并且甚至没有提供的例子来处理提取,所以我觉得有些东西在这里,但不是一个线索从哪里开始寻找.

在此先感谢您的帮助:)

解决方法

实际上,放置案例类代码存在差异.考虑两个例子:
class SomeServlet {
 def someMethod(json: String) = parse(json).extract[Person]
 case class Person(name: String,age: Int)
}

在这种情况下,类Person将具有类型为SomeServlet的隐式字段(类似于Java非静态内部类),因此不会对应于给定的JSON.这就是你得到例外的原因.现在看第二个例子:

class SomeServlet {
 def someMethod(json: String) = parse(json).extract[Person]
}

case class Person(name: String,age: Int)

在第二个示例中,Person是完全独立的,因此没有隐式字段.所以它应该工作.至少对我有用.

原文链接:https://www.f2er.com/js/150762.html

猜你在找的JavaScript相关文章