안드로이드(android) Gson(gson) 사용하기
안드로이드에서 Gson 사용하기
1. build.gradle(Module: app) -> depencies 에 추가하기
complie에서 implementation으로 명시하는 것으로 변경되었다고 한다.
implementation 'com.google.code.gson:gson:2.8.2'
2. 사용할 코드 부분에서 import를 한다.
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
Gson gson = new Gson(); // Gson을 사용하기 위한 선언
예시로 String -> JsonArray -> JsonArray값들 출력
1. String 값을 JsonArray로 변환
JsonArray reader = gson.fromJson(message , JsonArray.class);
// message : Json Array의 값을 JSON.stringify()화를 만든 것을
// 다시 JsonArray를 만듭니다.
2. JsonArray값 출력
JsonObject obj = null; // JsonObject 값을 출력하기 위한 선언
for (int i = 0; i < reader.size(); i++) { //jsonArray 사이즈 만큼 loop
obj = reader.get(i).getAsJsonObject();//jsonArray 배열 JsonObject를 가져옴
Log.d(getClass().getName() , obj.toString()); //toString으로 출력
}
# 참고 사이트 : https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
'[android]' 카테고리의 다른 글
안드로이드 마시멜로(API23) 이상 권한 체크 획득하기 (0) | 2018.12.30 |
---|---|
안드로이드 화면 회전 감지(Android rotation detection) (0) | 2018.11.24 |
안드로이드 웹뷰(WebView) 로컬 html 파일 열기 (0) | 2018.11.22 |
웹뷰(webview) 하얗게 나오는 경우(ssl인증 무시하기) (0) | 2018.11.20 |
안드로이드 webview 테스트하기 (0) | 2018.11.20 |