提问者:小点点

如何解析数组中的Json


我试图在array中解析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)

这是dataclass,array中的json,array的格式是

[
[
    {
        "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的时候,服务器是正常运行的,当这样的时候我应该如何修复它? 请帮忙,提前谢谢。


共2个答案

匿名用户

您需要更改代码,如下所示,因为响应具有给定模型数据的另一个列表,

 @POST("/storyGet")
 fun getStory() : Call<List<List<StoryDataSubList>>>

完整代码如下

@POST("/storyGet")
     fun getStory() : Call<List<List<StoryDataSubList>>>

方法调用Rovfit 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")
                }
            });
    
        }

匿名用户

arraylist替换为list,然后尝试。