버그 잡이

Kotlin + Retrofit + Moshi 중첩된 Json 풀기 #nested json 본문

안드로이드

Kotlin + Retrofit + Moshi 중첩된 Json 풀기 #nested json

버그잡이 2020. 4. 16. 14:48

 

Kotlin + Retrofit + Moshi 을 활용한 네트워크 통신 연습으로 Tistory API를 활용한 블로그 글 정보 json을 받아보기로 했다.

 

그런데.... 짜임새가 아주 거지같다.

 

{"tistory":{"status":"200","item":{"url":"https:\/\/jinsangjin.tistory.com","secondaryUrl":"","page":"1","count":"10","totalCount":"42","posts":[{"id":"43","title":"(AAC \uc751\uc6a9) LiveData+Room+RecyclerView #DiffUtil","postUrl":"https:\/\/jinsangjin.tistory.com\/43","visibility":"20","categoryId":"809598","comments":"0","trackbacks":"0","date":"2020-04-15 14:23:28"},{"id":"42","title":"(AAC \uc751\uc6a9)ViewModel+LiveData+Room\uc73c\ub85c ToDoList \uc571 \ub9cc\ub4e4\uae30","postUrl":"https:\/\/jinsangjin.tistory.com\/42","visibility":"20","categoryId":"809598","comments":"0","trackbacks":"0","date":"2020-04-14 20:38:45"},{"id":"41","title":"MutableLiveData<ArrayList<String>> live data\uc758 \ubcc0\ud654\ub97c observer\uc5d0\uac8c \uc54c\ub824\uc8fc\uc9c0 \ubabb \ud558\ub294 error","postUrl":"https:\/\/jinsangjin.tistory.com\/41","visibility":"20","categoryId":"0","comments":"0","trackbacks":"0","date":"2020-04-14 17:52:02"},{"id":"40","title":"[Udacity android with kotlin] 6. Room #ViewModel+LiveData+Room","postUrl":"https:\/\/jinsangjin.tistory.com\/40","visibility":"20","categoryId":"809598","comments":"0","trackbacks":"0","date":"2020-04-14 12:08:43"},{"id":"39","title":"\uc0bd\uc9c8 \ubc18\uc131 \uc77c\uae300413 - git pull request","postUrl":"https:\/\/jinsangjin.tistory.com\/39","visibility":"20","categoryId":"800561","comments":"0","trackbacks":"0","date":"2020-04-13 21:24:10"},{"id":"38","title":"\"\uc870\uc120\uc5d0\uc11c \ubc31\uc218\ub85c \uc0b4\uae30(\uace0\ubbf8\uc219 \uc9c0\uc74c)\" \uc744 \uc77d\uace0...","postUrl":"https:\/\/jinsangjin.tistory.com\/38","visibility":"20","categoryId":"808133","comments":"0","trackbacks":"0","date":"2020-04-12 17:26:49"},{"id":"37","title":"[Udacity android with kotlin] 5.Architectue - LiveData","postUrl":"https:\/\/jinsangjin.tistory.com\/37","visibility":"20","categoryId":"809598","comments":"0","trackbacks":"0","date":"2020-04-12 12:58:22"},{"id":"36","title":"(\uc2e4\uc804 \uc801\uc6a9)ViewModel + LiveData\ub85c \ud654\uba74 \uc804\ud658\uc2dc data \ucd08\uae30\ud654 \ubb38\uc81c \ud574\uacb0\ud558\uae30 #configuration change","postUrl":"https:\/\/jinsangjin.tistory.com\/36","visibility":"20","categoryId":"800558","comments":"0","trackbacks":"0","date":"2020-04-10 21:49:58"},{"id":"35","title":"[Udacity android with kotlin] 5. Architecture - viewmodel","postUrl":"https:\/\/jinsangjin.tistory.com\/35","visibility":"20","categoryId":"809598","comments":"0","trackbacks":"0","date":"2020-04-10 16:46:49"},{"id":"34","title":"[Udacity android with kotlin]4. LifeCycle #\uc548\ub4dc\ub85c\uc774\ub4dc \uc0dd\uba85\uc8fc\uae30","postUrl":"https:\/\/jinsangjin.tistory.com\/34","visibility":"20","categoryId":"800558","comments":"0","trackbacks":"0","date":"2020-04-10 12:17:41"}]}}}

 

정리하면 아래와 같은 구조이다.

 

{"tistory": { "item" : { "posts": [...] } }

 

내가 원하는 array에 닿기까지 jsonObject를 3번이나 풀어야 한다. 후...

 

data class를 다음과 같이 작성하여 해결하였다.

 

 

 

 

*data

 

- 원리는 간단하다. jsonObject가 중첩된 만큼 data class를 중첩해서 받아주면 된다.

data class ResponseData(
    @Json(name = "tistory")
    val tistory: Tistory
)

data class Tistory(
    @Json(name="item")
    val item: Item
)

data class Item(
    @field:Json(name = "posts")
    val posts: List<Article>
)

data class Article(
    val id: String,
    val title: String,
    val postUrl: String,
    val visibility: String,
    val categoryId: String,
    val comment: String,
    val trackbacks: String,
    val date: String
)

 

 

*retrofitAPI

 

interface BlogApiService {
    @GET("list?access_token=[본인 토큰 입력]&output=json&blogName=jinsangjin&page=1")
    fun getArticles(): Deferred<ResponseData>

}

 

 

 

*BlogViewModel

 

- 받아온 데이터(ResponseData형식) 에 .tistory.item.posts 를 통해서 내가 원하는 list를 가져올 수 있다.

 private fun getArticleList(){
        coroutineScope.launch {
            var getArticlesDeferred = BlogApi.retrofitService.getArticles()
            try{
                var listResult = getArticlesDeferred.await()
                _response.value = listResult.tistory.item.posts.size.toString()

            }catch (t: Throwable){
                _response.value = "Failure: " + t.message
            }
        }
    }

 

 

 

참고

https://android.developreference.com/article/11192450/Accessing+deeply+nested+JSON+array+with+Moshi+Retrofit2

반응형
Comments