无法投放Android Kotlin JSONObject

时间:2018-11-24 20:26:33

标签: android json kotlin

我正在尝试将JSON字符串转换为JSON数据类对象。编译器不会抱怨,Android Studio 3 IDE提供了必要的提示来更正代码。仍然出现运行时错误,提示无法转换JSONObject。请提供任何提示...

我有以下代码:

fun startReplay() {
    doAsync {
        triggerServerFile(eventDomain + "/get-replay-JSON.php")
        val jsonString = getServerFile(eventDomain + "/data/replay-SidR2018.json?nocache=" + Math.random())
        uiThread {
            var replayData:replayDataObject = JSONObject(jsonString) as replayDataObject
            val a= replayData.component1()
            val b= replayData.component2()[3].component3()
        }
    }
}

和以下数据类:

data class replayDataObject(
    val event: String,
    val shiptracks: List<Shiptrack>,
    val windtracks: List<Windtrack>
)

data class Shiptrack(
    val colorcode: String,
    val course: List<String>,
    val lat: List<String>,
    val lon: List<String>,
    val name: String,
    val speed: List<String>,
    val stamp: List<String>
)

data class Windtrack(
    val course: List<String>,
    val lat: List<String>,
    val lon: List<String>,
    val name: String,
    val speed: List<String>,
    val stamp: List<String>
)

最后是以下JSON字符串:

{
    "event": "SidR2018,Slag in de Rondte 2018",
    "shiptracks": [{
            "name": "Hollandia",
            "colorcode": "0",
            "stamp": ["1541407570", "1541407599"],
            "lat": ["53.173461666667", "53.17347"],
            "lon": ["5.409655", "5.409655"],
            "speed": ["0", "0"],
            "course": ["299", "301"]
        }, {
            "name": "Ouderzorg",
            "colorcode": "14",
            "stamp": ["1541407540", "1541407540", "1541407540"],
            "lat": ["53.17359", "53.17359", "53.17359"],
            "lon": ["5.409586", "5.409586", "5.409586"],
            "speed": ["0", "0", "0"],
            "course": ["0", "0", "0"]
        }
    ],
    "windtracks": [{
            "name": "Arcen",
            "stamp": ["1541409000", "1541411400"],
            "lat": ["51.5", "51.5"],
            "lon": ["6.2", "6.2"],
            "speed": ["4", "2"],
            "course": ["72", "61"]
        }, {
            "name": "Zeeplatform K13",
            "stamp": ["1541408400", "1541410800", "1541412000", "1541414400"],
            "lat": ["53.22", "53.22", "53.22", "53.22"],
            "lon": ["3.22", "3.22", "3.22", "3.22"],
            "speed": ["16", "16", "17", "16"],
            "course": ["126", "129", "127", "120"]
        }
    ]
}

堆栈跟踪(第381行是代码中包含JSOBObject的行: E / AndroidRuntime:致命异常:main     流程:nl.zeilvaartwarmond.ttandroid.szwtracktrace,PID:23370     java.lang.ClassCastException:org.json.JSONObject无法转换为nl.zeilvaartwarmond.ttandroid.szwtracktrace.MapsActivity $ replayDataObject         在nl.zeilvaartwarmond.ttandroid.szwtracktrace.MapsActivity $ startReplay $ 1 $ 1.invoke(MapsActivity.kt:381)         在nl.zeilvaartwarmond.ttandroid.szwtracktrace.MapsActivity $ startReplay $ 1 $ 1.invoke(MapsActivity.kt:44)         在org.jetbrains.anko.AsyncKt $ uiThread $ 1.run(Async.kt:70)         在android.os.Handler.handleCallback(Handler.java:898)         在android.os.Handler.dispatchMessage(Handler.java:107)         在android.os.Looper.loop(Looper.java:198)         在android.app.ActivityThread.main(ActivityThread.java:6716)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2 个答案:

答案 0 :(得分:0)

您需要能够解释JSON的内容才能将其映射到replayDataObject。在Kotlin中,data class可以很好地使用多种库中的任何一种。

Google Gson

https://github.com/google/gson

初始化Gson后,您只需将String取为:

gson.fromJson(jsonString, replayDataObject::class.java)

现在,提醒一下,您那里有很多非空类型,并且读入的数据或从中获得的数据可能有问题。

其他库:

答案 1 :(得分:0)

为您改装工作。 从服务器获取json并转换为对象。

https://square.github.io/retrofit/