gpt4 book ai didi

getting unexpected json token at offset 0 with kotlin.serialization(在偏移量为0的位置获得意外的json令牌,并使用kotlin.Serialization)

转载 作者:bug小助手 更新时间:2023-10-28 11:38:27 28 4
gpt4 key购买 nike



Trying to parse json string from here, however kotlin serialization is throwing this error:kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 0: Expected beginning of the string, but got { at path: $JSON input: {"events":[{"id":1

尝试从此处分析json字符串,但是kotlin序列化在偏移量0处引发了此error:kotlinx.serialization.json.internal.JsonDecodingException:意外的json令牌:预期的字符串开头,但得到了{at路径:$json输入:{“Events”:[{“id”:1


The json string is fetched with retro fit using this function:

JSON字符串是通过使用以下函数进行反向拟合来获取的:


@GET("bootstrap-static")
suspend fun getBootstrapJson(): String

and is then converted with this function in a custom json converter object:

然后在自定义json转换器对象中使用此函数进行转换:


fun convert(string: String): BootStrapModel{
return Json{ignoreUnknownKeys = true}.decodeFromString(string)
}

The boot strap model is defined like this:

引导模式的定义如下:


@Serializable
data class BootStrapModel(
@SerialName("events") val events: List<Events>,
@SerialName("game_settings") val gameSettings: GameSettings,
val phases: List<Phases>,
val teams: List<Team>,
@SerialName("total_players") val totalPlayers: Int,
val players: List<Player>,
@SerialName("element_stats") val elementStats: List<ElementStats>,
@SerialName("element_types") val elementTypes: List<ElementTypes>,
)

All custom data types here are serializable data classes with and data that I want.
any help is appreciated.

这里的所有定制数据类型都是可序列化的数据类,其中包含我想要的数据。如有任何帮助,我们不胜感激。


更多回答
优秀答案推荐

I believe this is happening because you are requesting a string result from your @GET suspend function call - getBootstrapJson(): String. The error is telling you that a string is the expected response type as per your request, but it is actually returning a JSON object. The JSON you are receiving shows that it is an object (the curly bracket '{'), you then have an array of objects titled events:

我相信这是因为您正在从@GET SUSPEND函数调用--getBootstrapJson():STRING请求字符串结果。该错误告诉您,根据您的请求,字符串是预期的响应类型,但它实际上返回了一个JSON对象。您收到的JSON显示它是一个对象(花括号‘{’),然后您就有了一个标题为Events的对象数组:


{
"events": [
{
"id": 1

Therefore, instead of requesting a string in your Retrofit suspend function, you should request an object which contains a list of event(s) e.g.

因此,与其在Retrofit暂停功能中请求字符串,不如请求一个包含事件列表的对象(S),例如


suspend fun getBootstrapJson(): BootStrapModel

Alternatively you could use a GSON converter -

或者,您可以使用GSON转换器-


implementation("com.google.code.gson:gson:2.10.1")
...

private val retrofit: Retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
...

Example model:

示例模型:


   data class BootStrapModel(
@SerializedName("events")
val events: List<Event> // Request for a list of Event objects
...
)

data class Event(
@SerializedName("id")
val id: Long,
@SerializedName("name")
...
)

data class ChipPlay (
@SerializedName("chip_name")
val chipName: ChipName,
@SerializedName("num_played")
val numPlayed: Long
)
...

There are websites such as https://app.quicktype.io/ where you can drop a copy of the JSON and it will produce the Kotlin classes required. You can also download a plugin to Android studio so that the classes are created inside your program file:
https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-

有一些网站,如https://app.quicktype.io/,您可以在其中放置JSON的副本,它将生成所需的Kotlin类。您还可以将一个插件下载到Android Studio,以便在您的程序文件https://plugins.jetbrains.com/plugin/9960-json-to-kotlin-class-jsontokotlinclass-中创建类


更多回答

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com