Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- oberve url
- transformation.map
- 상단 탭바
- base64 변환
- UIViewControllerTransitioningDelegate
- GeometryReader
- DevelopmentRegion
- Side Menu
- url 관찰
- ViewBuilder
- 기존 앱
- 스크롤 탭
- pod install
- development language
- notifychanged
- detect url
- scrolling tab
- 개발자 면접
- SwiftUI
- Tuist
- convert base64
- List
- ios
- Android
- swift #swift keychain #keychain 사용법
- Swift Package Manager
- url 추적
- UIPresentationController
- DataBinding
- swift
Archives
- Today
- Total
버그 잡이
Kotlin + Retrofit + Moshi 중첩된 Json 풀기 #nested json 본문
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
}
}
}
참고
반응형
'안드로이드' 카테고리의 다른 글
Room에서 List 사용하기 #Kotlin #TypeConverter #Android (2) | 2020.04.23 |
---|---|
EditText에서 한줄로 입력하기 #singleLine을 대체하는 inputType (0) | 2020.04.22 |
(실전 적용)ViewModel + LiveData로 화면 전환시 data 초기화 문제 해결하기 #configuration change (0) | 2020.04.10 |
[Udacity android with kotlin]4. LifeCycle #안드로이드 생명주기 (0) | 2020.04.10 |
[Android] 안드로이드 애니메이션 종류 #Property #View #Transition (1) | 2020.04.07 |
Comments