본문 바로가기

개발중/Java

json 문서 변환 (java)

728x90
반응형

package com.rsn.def.job;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;

public class MenuFile {

	List<menuVo> JsonList;
	
	public MenuFile() {
		
		JsonList = new ArrayList<menuVo>();
		
		Gson gson = new Gson();
		String path = "C:\\coding\\eclipse-workspace\\soo_MenuTest\\src\\main\\resources\\json\\menu.json";
		JsonReader rd = null;
		
		
		try {
			rd = new JsonReader(new FileReader(path));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		JsonObject obj = gson.fromJson(rd, JsonObject.class);
		JsonElement liststr = null;
		
		Set<Map.Entry<String, JsonElement>> element = obj.entrySet();
		
		for (Map.Entry<String, JsonElement> entry : element) {
			if (entry.getKey().equals("response_data")) {
				liststr = entry.getValue();
			}
		}

		menuVo[] array = gson.fromJson(liststr, menuVo[].class);
		JsonList = Arrays.asList(array);
	}
	
 
	public List<menuVo> getJsonList() {
		return JsonList;
	}
}

 

728x90
반응형

'개발중 > Java' 카테고리의 다른 글

Map 람다식  (0) 2021.04.16
카테고리 부모 자식 매핑  (0) 2021.04.13
재귀 도전 하다  (0) 2021.04.12
JAVA이클립스 라이브러리 추가하는 방법  (0) 2021.04.08
EC2 web 배포  (0) 2021.03.15