투케이2K

502. (Android/Java) [GSON] SerializedName 사용해 모델 클래스 JSON 생성 Key 명칭 설정 실시 본문

Android

502. (Android/Java) [GSON] SerializedName 사용해 모델 클래스 JSON 생성 Key 명칭 설정 실시

투케이2K 2023. 2. 23. 22:03
반응형

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

 

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

            // [M_Person 클래스 생성 실시]
            M_Person m_person = new M_Person("투케이", 29);


            // [GSON 사용해 Object to Json 변환 실시]
            String jsonString = new Gson().toJson(m_person);


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

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









// -------------------------------------
// [Model 클래스 생성]
// -------------------------------------
package com.example.javaproject;

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

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){
        this.name = name;
        this.age = age;
    }





    // -----------------------------------------------------------------------------------------
    // TODO [JSON KEY DATA]
    // -----------------------------------------------------------------------------------------
    @SerializedName("key_name")
    public String name;

    // -------------------------------------------

    @SerializedName("key_age")
    public int age;

    // -------------------------------------------


} // TODO [클래스 종료]

 


 

반응형
Comments