투케이2K

104. (TWOK/ALGORITHM) [Android] 문법 - [GSON] SerializedName 사용해 모델 클래스 생성 및 response 데이터 자동 파싱 수행 실시 본문

투케이2K 알고리즘

104. (TWOK/ALGORITHM) [Android] 문법 - [GSON] SerializedName 사용해 모델 클래스 생성 및 response 데이터 자동 파싱 수행 실시

투케이2K 2023. 2. 25. 22:19
반응형

[환경 설정 및 설명]

언 어 : Java

설 명 : 문법 - [GSON] SerializedName 사용해 모델 클래스 생성 및 response 데이터 자동 파싱 수행 실시

 

[소스 코드]

// -------------------------------------
// [로직 처리 실시]
// -------------------------------------
        try {

            // [String 샘플 JSON 데이터 생성]
            String sampleJson = "{\"key_age\":30,\"key_item\":[{\"key_lang\":\"Kotlin\",\"key_tool\":\"Android\"},{\"key_lang\":\"Swift\",\"key_tool\":\"Xcode\"}],\"key_name\":\"투케이\",\"key_note\":{\"key_blog\":\"twok.blog\"}}";


            // [M_Person 클래스 생성 실시]
            M_Person m_person = new Gson().fromJson(sampleJson, M_Person.class);


            // [M_Person 클래스에 매핑된 데이터 확인]
            String name = m_person.name;
            int age = m_person.age;
            String note = new Gson().toJson(m_person.note);
            String items = new Gson().toJson(m_person.items);


            // [로그 출력 실시]
            Log.i("---","---");
            Log.w("//===========//","================================================");
            Log.i("","\n"+"["+String.valueOf(ACTIVITY_NAME)+" >> onCreate() :: 로그 출력]");
            Log.i("","\n"+"[name :: "+String.valueOf(name)+"]");
            Log.i("","\n"+"[age :: "+String.valueOf(age)+"]");
            Log.i("","\n"+"[note :: "+String.valueOf(note)+"]");
            Log.i("","\n"+"[items :: "+String.valueOf(items)+"]");
            Log.w("//===========//","================================================");
            Log.i("---","---");

        }
        catch (Exception e){
            e.printStackTrace();
        }









// -------------------------------------
// [모델 클래스 생성 실시]
// -------------------------------------
package com.example.javaproject;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.HashMap;

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 [클래스 종료]
 

[결과 출력]

 

W///===========//: ================================================
I/: [A_Intro >> onCreate() :: 로그 출력]
I/: [name :: 투케이]
I/: [age :: 30]
I/: [note :: {"key_blog":"twok.blog"}]
I/: [items :: [{"key_lang":"Kotlin","key_tool":"Android"},{"key_lang":"Swift","key_tool":"Xcode"}]]
W///===========//: ================================================

 

반응형
Comments