这是API.kt的 Client.retro-6ren">
gpt4 book ai didi

android - 我应该如何解析数组中的数组中的Json

转载 作者:行者123 更新时间:2023-12-02 13:28:40 25 4
gpt4 key购买 nike

我试图解析数组中的数组中的Json,但是没有用。
来源是这样的

    @POST("/storyGet")
fun getStory() : Call<ArrayList<StoryData>>
这是API.kt的
Client.retrofitService.getStory().enqueue(object :
retrofit2.Callback<ArrayList<StoryData>> {
override fun onResponse(call: Call<ArrayList<StoryData>>?, response: Response<ArrayList<StoryData>>?) {
val repo = response!!.body()
when (response.code()) {
200 -> {
repo!!.indices.forEach {
items += StoryDataSubListItem(
repo[it][it][it].alreadyWatch,
repo[it][it][it].createdAt,
repo[it][it][it].imgUrl,
repo[it][it][it].storyUUID,
repo[it][it][it].userName,
repo[it][it][it].userName,
repo[it][it][it].userUUID
)
recyclerView!!.adapter?.notifyDataSetChanged()
}
}
}
}

override fun onFailure(call: Call<ArrayList<StoryData>>?, t: Throwable?) {
}
})
这是我的改造数据,
class StoryData : ArrayList<StoryDataSubList>()
这是第一个数组,
class StoryDataSubList : ArrayList<StoryDataSubListItem>()
这是我的第二个数组,
data class StoryDataSubListItem(
val alreadyWatch: List<Any>,
val createdAt: String,
val imgUrl: String,
val storyUUID: String,
val userName: String,
val userProfileImgUrl: String,
val userUUID: String)
这是数据类,数组中的json以数组的格式是
[
[
{
"alreadyWatch": [],
"createdAt": "test",
"_id": "_id",
"userUUID": "userUUID2",
"userName": "userName2",
"userProfileImgUrl": "false",
"imgUrl": "imageUrl",
"storyUUID": "StoryUUID",
"__v": 0
}
],
[
{
"alreadyWatch": [],
"createdAt": "test",
"_id": "_id",
"userUUID": "userUUID",
"userName": "TEST NAME",
"userProfileImgUrl": "false",
"imgUrl": "imageURL",
"storyUUID": "StoryUUID",
"__v": 0
}
]]
当我看到logcat时,服务器处于正常运行状态
何时需要修复?请帮助并提前感谢您。

最佳答案

您需要按以下方式更改代码,因为响应中包含给定模型数据的另一个列表,

 @POST("/storyGet")
fun getStory() : Call<List<List<StoryDataSubList>>>
完整代码如下
@POST("/storyGet")
fun getStory() : Call<List<List<StoryDataSubList>>>
调用Retrofit API的方法以获取如下响应
private fun loadStoryData() {
val call = netWorkApi.getStory() // replace netWorkApi with your retrofit API sevice

call.enqueue(object : Callback<List<List<StoryDataSubList>>> {
override fun onResponse(
call: Call<List<List<StoryDataSubList>>>,
response: Response<List<List<StoryDataSubList>>>
) {
if (response.isSuccessful) {
for (index in response.body()?.indices!!)
Log.d("TAG", "${response.body()!!.get(index)}")

}
}

override fun onFailure(call: Call<List<List<StoryDataSubList>>>, t: Throwable) {
TODO("Not yet implemented")
}
});

}

关于android - 我应该如何解析数组中的数组中的Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62678096/

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