gpt4 book ai didi

android - Moshi - 将字符串转换为对象

转载 作者:行者123 更新时间:2023-11-30 04:59:55 25 4
gpt4 key购买 nike

我不知道如何在 kotlin 中执行此操作。我想将此字符串转换为对象数组:

[
{mode=bus, line=381, line_name=381, direction=Waterloo, operator=CX, date=2019-10-16, expected_departure_date=2019-10-16, aimed_departure_time=20:11, expected_departure_time=20:13, best_departure_estimate=20:13, source=Countdown instant, dir=outbound, operator_name=ABELLIO LONDON LIMITED, id=https://transportapi.com/v3/uk/bus/route/CX/381/outbound/490010596E/2019-10-16/20:11/timetable.json?app_id=f56baf6a&app_key=6b0d065a1465ccbb6f32438f3d960803},

{mode=bus, line=381, line_name=381, direction=Waterloo, operator=CX, date=2019-10-16, expected_departure_date=2019-10-16, aimed_departure_time=20:33, expected_departure_time=20:34, best_departure_estimate=20:34, source=Countdown instant, dir=outbound, operator_name=ABELLIO LONDON LIMITED, id=https://transportapi.com/v3/uk/bus/route/CX/381/outbound/490010596E/2019-10-16/20:33/timetable.json?app_id=f56baf6a&app_key=6b0d065a1465ccbb6f32438f3d960803}
]

我也想转换的对象:

@JsonClass(generateAdapter = true)
data class BusLive(val aimed_departure_time: String, val best_departure_estimate: String, val date: String, val dir: String,
val direction: String,
val expected_departure_date: String,
val expected_departure_time: String,
val id: String,
val line: String,
val line_name: String,
val mode: String,
val operator: String,
val operator_name: String,
val source: String
)

我试过:

  fun moshiConverter(test:String) : String {
val moshi = Moshi.Builder().build()
val type = Types.newParameterizedType(LiveBus::class.java, LiveBus::class.java)
val adapter:JsonAdapter<String> = moshi.adapter(type)
val a = adapter.toJson(test)
return a
}

没有快乐:(

最佳答案

我希望您发布的 JSON 格式只是一个例子。因为它不是有效的 JSON 字符串。

要修复您的示例,您必须更改几行:

    val type = Types.newParameterizedType(LiveBus::class.java, LiveBus::class.java)
val adapter:JsonAdapter<String> = moshi.adapter(type)

    val type = Types.newParameterizedType(List::class.java, LiveBus::class.java)
val adapter = moshi.adapter(type)

关于android - Moshi - 将字符串转换为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58420437/

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