问题描述
我正在发出一个API请求,该请求返回一些数组值.我需要序列化这些数组值,以便可以将它们分配给它们相应的类属性(即String类型).
I am making an API request which returns some array values. I need to serialize these array values so that I can assign them to their corresponding class attributes (which are String types).
现在,我知道如何使用GSON对列表进行序列化和反序列化,但是使用Retrofit,映射将自动完成.这意味着,如果我的属性的类型为String,则API调用将返回错误期望字符串但接收到数组".我该如何解决这个问题,以便我可以毫无故障地将它们作为数组接收,并随后将它们存储为字符串?
Now I know how to use GSON to serialize and deserialize lists, but with Retrofit the mapping is done automatically. This means that if my attribute is of type String, the API call returns the error "Expected a String but received an Array instead". How do I get around this so that I can receive them as arrays without failure, and them store them as strings subsequently?
我的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类:
My UtteranceResponse class:
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接口也是如此:
UPDATEMy API Interface as well:
@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>
更改为
You are returning single object not list. Change Call<UtteranceResponse>
in ApiInterface to
Call<List<Utterance>>
并将列表转换为字符串将列表转换为字符串并将字符串转换为列表
and for converting list to string list to string and string to list
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(){
}
}
这篇关于如何获得GSON&是否可以进行序列化并将数组从JSON响应映射到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!