shimhg02
shimhg02

Reputation: 31

How Should I Parsing Json In Array In Array

I was trying to parse Json in array in array But It doesn't worked. Source was like this

    @POST("/storyGet")
fun getStory() : Call<ArrayList<StoryData>>

this is API.kt's and

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?) {
        }
    })

This is my Retrofit Data and

class StoryData : ArrayList<StoryDataSubList>()

this is first Array and

class StoryDataSubList : ArrayList<StoryDataSubListItem>()

this is my second array and

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)

this is dataclass and json in array in array's format is

[
[
    {
        "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
    }
]]

When I saw logcat, server was in normal operation when like this how should I fix It? please help and thank you in advance.

Upvotes: 1

Views: 99

Answers (2)

Krishna Upadhya
Krishna Upadhya

Reputation: 151

You need to change your code as below, since the response has list of another list of the given model data,

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

Full code is as below

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

method calling retrofit API to get the response as below

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")
                }
            });
    
        }

Upvotes: 1

maryam
maryam

Reputation: 1505

Replace ArrayList with List and try it.

Upvotes: 0

Related Questions