투케이2K

103. (TWOK/ALGORITHM) [Android] 문법 - [GSON] SerializedName 사용해 모델 클래스 JsonObject , JsonArray 생성 본문

투케이2K 알고리즘

103. (TWOK/ALGORITHM) [Android] 문법 - [GSON] SerializedName 사용해 모델 클래스 JsonObject , JsonArray 생성

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

[환경 설정 및 설명]

언 어 : Java

설 명 : 문법 - [GSON] SerializedName 사용해 모델 클래스 JsonObject , JsonArray 생성

 

[소스 코드]

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

            // [JSON 생성에 필요한 데이터 선언]
            M_Person.Note note = new M_Person.Note("twok.blog");


            // [JSON 생성에 필요한 데이터 선언]
            ArrayList<M_Person.Item> items = new ArrayList<>();
            items.add(new M_Person.Item("Java", "Android"));
            items.add(new M_Person.Item("Swift", "Xcode"));


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


            // [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();
        }









// -------------------------------------
// [모델 클래스 생성 실시]
// -------------------------------------
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/: [jsonString :: {"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"}}]
W///===========//: ================================================

 

반응형
Comments