我正在尝试将 JSON 对象数组从键转换为 Scala 中的对象数组...
这是代码:
case class RoomList(val rooms : Array[Room])
case class Room(val name : String)
val json = "{\"rooms\" : [{\"name\" : \"Test\"}]}"
println(json.unpickle[RoomList])
这是例外
Exception in thread "main" java.lang.InstantiationException: [Lhipchat.Room;
at sun.misc.Unsafe.allocateInstance(Native Method)
at hipchat.HipChat$HipchatRoomListUnpickler1$2$ScalaArray$u005BhipchatRoom$u005DUnpickler1$2$.unpickle(HipChat.scala:46)
at hipchat.HipChat$HipchatRoomListUnpickler1$2$.unpickle(HipChat.scala:46)
at hipchat.HipChat.getRooms(HipChat.scala:46)
at bot.Bot$.main(Bot.scala:11)
at bot.Bot.main(Bot.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)
这是一个房间列表:
val room = new Room("test")
val rooms = Array(room)
val rl = new RoomList(rooms)
println(rl.pickle)
腌制:
JSONPickle({
"tpe": "hipchat.RoomList",
"rooms": {
}
})
还有一个腌制的房间:
JSONPickle({
"tpe": "hipchat.Room",
"name": "test"
})