android - 如何让 GSON 和 Retrofit 序列化数组并将其从 JSON 响应映射到字符串?
问题描述
我正在发出一个返回一些数组值的 API 请求。我需要序列化这些数组值,以便我可以将它们分配给它们相应的类属性(它们是字符串类型)。
现在我知道如何使用 GSON 来序列化和反序列化列表,但是使用 Retrofit 映射是自动完成的。这意味着,如果我的属性是字符串类型,API 调用会返回错误“预期为字符串,但收到了数组”。我该如何解决这个问题,以便我可以将它们作为数组接收而不会失败,然后它们随后将它们存储为字符串?
我的 API 响应:
{
"utterances": [{
"langs": ["eng", "afr", "xho", "zul"],
"utts": [
"Have you been here before?",
"Was u al hier gewees?",
"Ingaba wakhe weza apha ngaphambili?",
"Ingabe uke weza lapha ngaphambilini?"
],
"responses": [
["Yes", "No"],
["Ja", "Nee"],
["Ewe", "Hayi"],
["Yebo", "Cha"]
]
},
{
"langs": ["eng", "afr", "xho", "zul"],
"utts": [
"How are you?",
"Hoe gaan dit met jou?",
"unjani?",
"unjani?"
],
"responses": [
["Good", "Bad"],
["Goed", "sleg"],
["ezilungileyo", "ezimbi"],
["kuhle", "kubi"]
]
}
]
}
我的 UtteranceResponse 类:
class UtteranceResponse {
@SerializedName("status")
var status: String? = null
@SerializedName("count")
var count: Int = 0
@SerializedName("utterances")
var utterances: ArrayList<Utterance>? = null
}
我的演讲课:
class Utterance: SugarRecord {
@SerializedName ("langs")
var langs: String? = null
@SerializedName ("utts")
var utterances_text: String? = null
var utterances_tts: String? = null
@SerializedName ("responses")
var responses_text: String? = null
constructor(){
}
}
最后是调用函数:
fun getUtterancesFromWebservice (){
val apiService = ApiInterface.create()
val call = apiService.getUtteranceDetails()
call.enqueue(object: Callback<UtteranceResponse> {
override fun onResponse(call: Call<UtteranceResponse>, response: retrofit2.Response<UtteranceResponse>?) {
if (response != null) {
if (response.body()?.utterances != null){
var list: List<Utterance> = response.body()?.utterances!!
val utterances: Utterance = list[0]
//storeUtterancesFromList(list)
} else {
Log.d ("Response:", response.body().toString())
}
}else{
Log.d ("responseResult", "NULL")
}
}
override fun onFailure(call: Call<UtteranceResponse>, t: Throwable) {
Log.e("SHIT", t.toString())
}
})
}
更新 我的 API 接口:
@GET("bins/1ahazo")
abstract fun getUtteranceDetails():Call<UtteranceResponse>
companion object Factory {
const val BASE_URL = "https://api.myjson.com/"
fun create(): ApiInterface {
val gson = GsonBuilder().setPrettyPrinting().create()
val retrofit = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
return retrofit.create(ApiInterface::class.java)
}
}
解决方案
您正在返回单个对象而不是列表。ApiInterface更改Call<UtteranceResponse>
为
Call<List<Utterance>>
以及将列表转换为字符串列表转换为字符串和字符串转换为列表
class Utterance: SugarRecord {
@SerializedName ("langs")
var langs: List<String?>? = null
@SerializedName ("utts")
var utterances_text: String? = null
var utterances_tts: List<String?>? = null
@SerializedName ("responses")
var responses_tex:List<List<String?>?>? = null;
constructor(){
}
}
推荐阅读
- c - Keil uVision 调试步骤通过项目外的代码
- python - 使用 boto3 将文件上传到 Amazon S3 存储桶中的特定位置?
- streamsets - 特殊字符(重音、撇号、trema)在自定义源测试中有效,但在部署在 dockerized Streamsets 中时不再有效
- c# - 使用 C# 检查一组不断变化的整数范围
- java - 多上下文 spring-boot 应用程序:如何为每个子上下文定义标准 spring-boot 属性
- oop - 带有材料清单的课程:最佳实践
- list - 返回列表的第 n 个元素
- reactjs - React 路由器是正确的,但不能按预期工作
- cygwin - 兼容性层与虚拟化层
- r - 数据表 lapply 和输出中的附加列