Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 如何解析ktor中发布的原始json数据列表_Kotlin_Ktor - Fatal编程技术网

Kotlin 如何解析ktor中发布的原始json数据列表

Kotlin 如何解析ktor中发布的原始json数据列表,kotlin,ktor,Kotlin,Ktor,我正在发布对象的json数组。我试着用这样的代码来解析它 val objs = call.receive<List<MyClass>>() // this work fine val name objs[0].name // this throw exception LinkedTreeMap cannot be cast to MyClass val json = call.receive<String>() val objs = Gson().fromJ

我正在发布对象的json数组。我试着用这样的代码来解析它

val objs = call.receive<List<MyClass>>() // this work fine
val name objs[0].name // this throw exception LinkedTreeMap cannot be cast to MyClass
val json = call.receive<String>()
val objs = Gson().fromJson(json, Array<MyClass>::class.java)
objs[0].name
suspend inline fun <reified T> ApplicationCall.safeReceive(): T {
    val json = this.receiveOrNull<String>()
    return Gson().fromJson(json, T::class.java)
}
val objs = call.safeReceive<Array<MyClass>>()
objs[0].name
val objs=call.receive()//这项工作很好
val name objs[0]。name//无法将此抛出异常LinkedTreeMap强制转换为MyClass
在上面的代码中,第二行抛出异常
com.google.gson.internal.LinkedTreeMap无法转换为MyClass

如果我发布简单对象并在ktor中使用
call.receive()
解析它,那么它就可以正常工作。所以问题只在解析对象列表时出现。

您可以这样做

val objs = call.receive<List<MyClass>>() // this work fine
val name objs[0].name // this throw exception LinkedTreeMap cannot be cast to MyClass
val json = call.receive<String>()
val objs = Gson().fromJson(json, Array<MyClass>::class.java)
objs[0].name
suspend inline fun <reified T> ApplicationCall.safeReceive(): T {
    val json = this.receiveOrNull<String>()
    return Gson().fromJson(json, T::class.java)
}
val objs = call.safeReceive<Array<MyClass>>()
objs[0].name
val json=call.receive()
val objs=Gson().fromJson(json,Array::class.java)
objs[0]。名称
已更新

您也可以像这样创建扩展函数

val objs = call.receive<List<MyClass>>() // this work fine
val name objs[0].name // this throw exception LinkedTreeMap cannot be cast to MyClass
val json = call.receive<String>()
val objs = Gson().fromJson(json, Array<MyClass>::class.java)
objs[0].name
suspend inline fun <reified T> ApplicationCall.safeReceive(): T {
    val json = this.receiveOrNull<String>()
    return Gson().fromJson(json, T::class.java)
}
val objs = call.safeReceive<Array<MyClass>>()
objs[0].name
suspend inline fun ApplicationCall.safeReceive():T{
val json=this.receiveOrNull()
返回Gson().fromJson(json,T::class.java)
}
然后像这样使用它

val objs = call.receive<List<MyClass>>() // this work fine
val name objs[0].name // this throw exception LinkedTreeMap cannot be cast to MyClass
val json = call.receive<String>()
val objs = Gson().fromJson(json, Array<MyClass>::class.java)
objs[0].name
suspend inline fun <reified T> ApplicationCall.safeReceive(): T {
    val json = this.receiveOrNull<String>()
    return Gson().fromJson(json, T::class.java)
}
val objs = call.safeReceive<Array<MyClass>>()
objs[0].name
val objs=call.safeReceive()
objs[0]。名称

将代码与
数组
一起使用,而不是
列表
使用ktor v1.2.3对我有效:

val objs = call.receive<Array<MyClass>>() 
val name = objs[0].name

参考资料:

谢谢您的快速回复,我会试试的。如果可能的话,不做手工解析会更好,但总比不做要好。感谢agianThis一直在工作,但在将ktor从v1.1.3更新到v1.2.3之后,它就不再工作了。。