Kotlin Gson演员表

时间:2018-10-29 13:38:19

标签: android kotlin

我正在尝试使用Java将JSON转换为Kotlin中的列表,但是会引发一些奇怪的错误。我正在Android虚拟机上运行此代码。

代码:

val json = Ion.with(applicationContext)
.load("$url/get/entries")
.asString()
.get()
println(json)
var entries  = gson.fromJson(json, mutableListOf<Entry>().javaClass)
((entries as MutableList<Entry>)[0] as Entry).copy()

Stacktrace:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: carson.com.scoutingapp, PID: 22484
java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to carson.com.scoutingapp.Entry
    at carson.com.scoutingapp.MainActivity.switchToStats(MainActivity.kt:66)
    at carson.com.scoutingapp.MainActivity.access$switchToStats(MainActivity.kt:25)
    at carson.com.scoutingapp.MainActivity$switchToMain$3.onClick(MainActivity.kt:42)
    at android.view.View.performClick(View.java:5610)
    at android.view.View$PerformClick.run(View.java:22265)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

我的JSON看起来像这样:

I/System.out: [
  {
    "cubesInClaim": 1,
    "cubesStolen": 2,
    "cubesInCargo": 3,
    "onCraterEdge": true,
    "inCrater": false,
    "climbed": false,
    "auto": {
      "teamNumber": 1,
      "team": "RED",
      "position": "CLAIM",
      "climbed": false,
      "cubePos": "RIGHT",
      "sampled": true,
      "failedSample": false,
      "onCrater": false,
      "claimedDepo": false
    }
  },
  {
    "cubesInClaim": 10,
    "cubesStolen": 10,
    "cubesInCargo": 10,
    "onCraterEdge": false,
    "inCrater": false,
    "climbed": true,
    "auto": {
      "teamNumber": 2,
      "team": "RED",
      "position": "CRATER",
      "climbed": true,
      "cubePos": "LEFT",
      "sampled": true,
      "failedSample": false,
      "onCrater": false,
      "claimedDepo": true
    }
  },
  {
    "cubesInClaim": 5,
    "cubesStolen": 5,
    "cubesInCargo": 5,
    "onCraterEdge": false,
    "inCrater": true,
    "climbed": false,
    "auto": {
      "teamNumber": 3,
      "team": "BLUE",
      "position": "CLAIM",
      "climbed": false,
      "cubePos": "MIDDLE",
      "sampled": false,
      "failedSample": true,
      "onCrater": false,
      "claimedDepo": false
    }
  },
  {
    "cubesInClaim": 1,
    "cubesStolen": 10,
    "cubesInCargo": 1,
    "onCraterEdge": false,
    "inCrater": false,
    "climbed": true,
    "auto": {
      "teamNumber": 4,
      "team": "BLUE",
      "position": "CRATER",
      "climbed": true,
      "cubePos": "MIDDLE",
      "sampled": false,
      "failedSample": false,
      "onCrater": false,
      "claimedDepo": true
    }
  }
]

我的Entry类如下:

data class AutoEntry(val teamNumber :Int,
                 val team :Team,
                 val position: Position,
                 val climbed :Boolean,
                 val cubePos :CubePos,
                 val sampled :Boolean,
                 val failedSample :Boolean,
                 val onCrater : Boolean,
                 val claimedDepo :Boolean){
companion object {
    val gson = GsonBuilder().setPrettyPrinting().create()
}

override fun toString(): String {
    return gson.toJson(this)
}
}
data class Entry(
val cubesInClaim :Int,
val cubesStolen :Int,
val cubesInCargo :Int,
val onCraterEdge :Boolean,
val inCrater :Boolean,
val climbed :Boolean,
val auto : AutoEntry,
@Transient val teamNumber: Int = auto.teamNumber,
@Transient val team: Team = auto.team,
@Transient val position: Position = auto.position){
override fun toString(): String {
    return AutoEntry.gson.toJson(this)
}
}

我尝试了几种强制类型转换,例如对java.util.ArrayList,mutableList和list的不同实现

我相信错误是在尝试调用Entry#copy()时发生的,但是无论我调用哪种方法,它都会引发异常。

编辑:同时为Entry和AutoEntry使用默认构造函数不起作用

0 个答案:

没有答案