Notice
Recent Posts
Recent Comments
Link
투케이2K
518. (Android/Java) [Observable] [GSON] JsonObject , JsonArray 데이터 Model SerializedName 매핑 및 데이터 확인 본문
Android
518. (Android/Java) [Observable] [GSON] JsonObject , JsonArray 데이터 Model SerializedName 매핑 및 데이터 확인
투케이2K 2023. 3. 14. 07:09[개발 환경 설정]
개발 툴 : AndroidStudio
[소스 코드]
// -------------------------------------
// [호출 소스 코드]
// -------------------------------------
try {
// [샘플 JSON 데이터 생성 실시]
String jsonData = "{\"key_age\":29,\"key_item\":[{\"key_lang\":\"Java\",\"key_tool\":\"Android\"},{\"key_lang\":\"Swift\",\"key_tool\":\"Xcode\"}],\"key_name\":\"투케이\",\"key_note\":{\"key_blog\":\"twok.blog\"}}\n";
// [Observable just 사용해 생성 실시 : 생성자]
Observable<M_Person> observable = Observable.just(new Gson().fromJson(jsonData, M_Person.class));
// [Observable 생성자 를 구독 (Subscribe) 해 데이터를 소비 : value 값은 M_Person 모델 클래스]
observable.subscribe(
value -> {
Log.i("---","---");
Log.d("//===========//","================================================");
Log.i("","\n"+"[A_Test >> main() :: onNext()]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[value :: "+String.valueOf(new Gson().toJson(value))+"]");
Log.d("//===========//","================================================");
Log.i("---","---");
// [세부 데이터 파싱]
String name = value.name;
String age = String.valueOf(value.age);
String note = new Gson().toJson(value.note);
String items = new Gson().toJson(value.items);
Log.i("---","---");
Log.d("//===========//","================================================");
Log.i("","\n"+"[A_Test >> main() :: parsing]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[name :: "+String.valueOf(name)+"]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[age :: "+String.valueOf(age)+"]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[note :: "+String.valueOf(note)+"]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[items :: "+String.valueOf(items)+"]");
Log.d("//===========//","================================================");
Log.i("---","---");
},
error -> {
Log.i("---","---");
Log.e("//===========//","================================================");
Log.i("","\n"+"[A_Test >> main() :: onError()]");
Log.i("","\n"+"-----------------------------------------");
Log.i("","\n"+"[error :: "+String.valueOf(error.getMessage())+"]");
Log.e("//===========//","================================================");
Log.i("---","---");
},
() -> {
Log.i("---","---");
Log.w("//===========//","================================================");
Log.i("","\n"+"[A_Test >> main() :: onComplete()]");
Log.w("//===========//","================================================");
Log.i("---","---");
});
}
catch (Exception e){
e.printStackTrace();
}
// -------------------------------------
// [M_Person 모델 클래스 소스 코드]
// -------------------------------------
package com.example.javaproject;
import com.google.gson.annotations.SerializedName;
import java.util.ArrayList;
public class M_Person {
/**
* // --------------------------------------------------------------------------------------
* TODO [클래스 설명]
* // --------------------------------------------------------------------------------------
* 1. GSON 모델 클래스
* // --------------------------------------------------------------------------------------
* 2. @SerializedName : JSON 객체 생성 시 표시될 key name
* // --------------------------------------------------------------------------------------
* 3. @Expose : 데이터가 null 일 경우 json 생성 시 제외 설정
* // --------------------------------------------------------------------------------------
* 4. build.gradle 설정 :
*
* implementation 'com.google.code.gson:gson:2.8.6'
* // --------------------------------------------------------------------------------------
* */
/**
* // --------------------------------------------------------------------------------------
* // TODO [빠른 로직 찾기 : 주석 로직 찾기]
* // --------------------------------------------------------------------------------------
* // [SEARCH FAST] :
* // --------------------------------------------------------------------------------------
* */
// -----------------------------------------------------------------------------------------
// TODO [전역 변수 선언]
// -----------------------------------------------------------------------------------------
private static final String ACTIVITY_NAME = "M_Person";
// -----------------------------------------------------------------------------------------
// TODO [클래스 생성자 초기화]
// -----------------------------------------------------------------------------------------
public M_Person(String name, int age, Note note, ArrayList<Item> items){
this.name = name;
this.age = age;
this.note = note;
this.items = items;
}
// -----------------------------------------------------------------------------------------
// TODO [JSON KEY DATA]
// -----------------------------------------------------------------------------------------
// [JSON KEY]
@SerializedName("key_name") public String name;
// -------------------------------------------
// [JSON KEY]
@SerializedName("key_age") public int age;
// -------------------------------------------
// [JSON KEY]
@SerializedName("key_note") public Note note;
// -------------------------------------------
// [JSON KEY]
@SerializedName("key_item") public ArrayList<Item> items;
// -------------------------------------------
// [JSON OBJECT]
public static class Note {
public Note(String blog){
this.blog = blog;
}
@SerializedName("key_blog") public String blog;
}
// -------------------------------------------
// [JSON ARRAY]
public static class Item {
public Item(String lang, String tool){
this.lang = lang;
this.tool = tool;
}
@SerializedName("key_lang") public String lang;
@SerializedName("key_tool") public String tool;
}
// -------------------------------------------
} // TODO [클래스 종료]
[결과 출력]
D///===========//: ================================================
I/: [A_Test >> main() :: onNext()]
I/: -----------------------------------------
I/: [value :: {"key_age":29,"key_item":[{"key_lang":"Java","key_tool":"Android"},{"key_lang":"Swift","key_tool":"Xcode"}],"key_name":"투케이","key_note":{"key_blog":"twok.blog"}}]
D///===========//: ================================================
I/---: ---
I/---: ---
D///===========//: ================================================
I/: [A_Test >> main() :: parsing]
I/: -----------------------------------------
I/: [name :: 투케이]
I/: -----------------------------------------
I/: [age :: 29]
I/: -----------------------------------------
I/: [note :: {"key_blog":"twok.blog"}]
I/: -----------------------------------------
I/: [items :: [{"key_lang":"Java","key_tool":"Android"},{"key_lang":"Swift","key_tool":"Xcode"}]]
D///===========//: ================================================
반응형
'Android' 카테고리의 다른 글
Comments