Notice
Recent Posts
Recent Comments
Link
투케이2K
281. (AndroidStudio/android/java) GSON 사용해 Map , Json 데이터 변환 수행 실시 본문
[개발 환경 설정]
개발 툴 : AndroidStudio
개발 언어 : java
[소스 코드]
package com.example.testapp;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Test {
public static void main(String[] args){
System.out.println("[Program Start]");
System.out.println("");
// ===================================
// TODO [설치 필요한 의존성 패키지]
// implementation 'com.google.code.gson:gson:2.8.6'
// ===================================
// TODO [GSON 사용해 JSON 데이터를 Map 으로 변환 수행 실시]
String sampleJson = "{\n" +
" \"name\" : \"TWOK\",\n" +
" \"age\" : \"29\"\n" +
"}";
// [데이터 출력 실시]
System.out.println("[JSON 원본] sampleJson : " + sampleJson);
System.out.println("");
// [GSON 객체 선언 실시]
Gson parsing_gson = new Gson(); // [일반 GSON 생성]
//Gson parsing_gson = new GsonBuilder().setPrettyPrinting().create(); // [PrettyPrinting 옵션을 추가 데이터 이쁘게 출력]
// [JSON 데이터를 MAP 으로 변환 수행 실시]
Map<String, Object> map = parsing_gson.fromJson(sampleJson, Map.class);
// [데이터 출력 실시]
System.out.println("[Map 변환] map : " + map.toString());
System.out.println("");
// ===================================
// TODO [GSON 사용해 Map 데이터를 JSON 으로 변환 수행 실시]
// [GSON 객체 선언 실시]
Gson create_gson = new Gson(); // [일반 GSON 생성]
//Gson create_gson = new GsonBuilder().setPrettyPrinting().create(); // [PrettyPrinting 옵션을 추가 데이터 이쁘게 출력]
// [JSON 데이터 만들기 실시]
String jsonString = create_gson.toJson(map); // [Map 객체 삽입]
// [데이터 출력 실시]
System.out.println("[JSON 생성] jsonString : " + jsonString);
System.out.println("");
// ===================================
} // [메인 종료]
} // [클래스 종료]
[결과 출력]
반응형
'Android' 카테고리의 다른 글
Comments