2016-07-28 50 views
0

我正在使用Scala的Gson庫。我的JSON看起來像GSon fromJson無法在Scala中使用陣列

{"response":{"id":"xyz","userName":"foo","firstName":"foo","lastName":"bar","groups":["A", "B", "C"]}, "count":1} 

解析這個我寫了下面的代碼

case class MyAdmin(id: String, userName: String, firstName: String, lastName: String, groups: List[String]) 
case class MyResponse(response: MyAdmin, count: Int) 

我的代碼解析的樣子

val respScala = gson.fromJson(responseString, classOf[MyResponse]) 

,但我得到一個錯誤

Exception in thread "main" java.lang.RuntimeException: Failed to invoke public scala.collection.immutable.List() with no args 
    at com.google.gson.internal.ConstructorConstructor$3.construct(ConstructorConstructor.java:111) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:210) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220) 
    at com.google.gson.Gson.fromJson(Gson.java:887) 
    at com.google.gson.Gson.fromJson(Gson.java:852) 
    at com.google.gson.Gson.fromJson(Gson.java:801) 
    at com.google.gson.Gson.fromJson(Gson.java:773) 
    at com.researchnow.FlareCommandLine$.delayedEndpoint$com$researchnow$FlareCommandLine$1(FlareCommandLine.scala:28) 
    at com.researchnow.FlareCommandLine$delayedInit$body.apply(FlareCommandLine.scala:17) 
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34) 
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) 
    at scala.App$$anonfun$main$1.apply(App.scala:76) 
    at scala.App$$anonfun$main$1.apply(App.scala:76) 
    at scala.collection.immutable.List.foreach(List.scala:381) 

回答

0

做了一些研究,發現Gs on不能很好地處理scala集合。我用Json4s取代了Gson ...現在一切正常,我能夠輕鬆地反序列化我的json。

有人必須告訴Alvin Alexander先生更新他的博客。他使用了很多舊的工具和代碼,但谷歌會一直指向他的博客。