2017-06-06 31 views
0

考慮:是否有開箱即用json4s解析2元組列表的方式?

implicit val formats = DefaultFormats 

val json = 
    """[ 
     {"myType":"type1","things":["1","2","3","4","5","6"]}, 
     {"myType":"type1","things":["1","2","3","4","5","6"]} 
    ]""" 

case class Stuff(myType: String, things: List[String]) 
parse(json).extract[List[Stuff]] 

斯卡拉在運行時會產生這個錯誤:

An exception or error caused a run to abort: Temp and Temp$$anonfun$1$Stuff$3 disagree on InnerClasses attribute 
java.lang.IncompatibleClassChangeError: Temp and Temp$$anonfun$1$Stuff$3 disagree on InnerClasses attribute 

是否有超出現成的方式與json4s解析這個,或者這是一個自定義的情況下,串行?

json4s版本3.5.2

斯卡拉不是最新版本,由於系統的限制,2.10.4

回答

1

你有範圍的隱含Formats對象?

extract方法需要隱式格式參數。 Json4s爲列表提供了默認格式。

下面的代碼對我的作品

import org.json4s.DefaultFormats 
import org.json4s.native.JsonMethods.parse 

implicit val formats = DefaultFormats 

val json = 
    """[ 
     {"myType":"type1","things":["1","2","3","4","5","6"]}, 
     {"myType":"type1","things":["1","2","3","4","5","6"]} 
    ]""" 

case class Stuff(myType: String, things: List[String]) 
parse(json).extract[List[Stuff]] 


res0: List[Stuff] = List(Stuff(type1,List(1, 2, 3, 4, 5, 6)), Stuff(type1,List(1, 2, 3, 4, 5, 6))) 

json4s版本3.2.11

階版本2.11

如果你有在範圍內隱格式化它可能會在json4s反射器中的錯誤。在不同的地方聲明您的Stuff案例類可能會解決問題。有關詳細信息,請參閱此github問題https://github.com/json4s/json4s/issues/84

+0

我的問題沒有明確說明我正在使用隱式。 ;)它的確如此,我將更新這個問題。我也會嘗試移動案例課。 –

+0

將案例類移到主類之外解決了問題。有趣。斯卡拉酷,但有時... –

+0

好吧,那麼爲什麼會發生這種情況?這是否有一個很好的理由,或者它是一個錯誤的地方? –

相關問題