GSON – How to parse input JSON with dynamic keys :: 행복한 프로그래머

posted by 쁘로그램어 2018. 5. 28. 20:26

json 파일에서 key 값이 동적인 경우 파싱하는 방법이다.


# 구글 검색

GSON – How to parse input JSON with dynamic keys

JSON - deserialization of dynamic object using Gson

Parse Json Object with dynamic keys using Gson

gson dynamic key name



# 테스트

public class DynamicJsonApp {

public static void main(String[] args) throws IOException {

// TODO Auto-generated method stub


Gson gson = new GsonBuilder().setPrettyPrinting().create();


String strData = new String(Files.readAllBytes(Paths.get("test.json")));

DemoJson myObj  = gson.fromJson(strData, new TypeToken<DemoJson>(){}.getType());


System.out.println("###########################################");

System.out.println(myObj);


System.out.println("###########################################");

System.out.println(myObj.getData().getFeeds().get("39").getAddress());

System.out.println(myObj.getData().getFeeds().get("39").getName());

Map<String, Feeds> feeds = myObj.getData().getFeeds();

Set<String> keySet = feeds.keySet();

Iterator<String> it = keySet.iterator();

while (it.hasNext()) {

             String key = it.next();

             Feeds value = feeds.get(key);

             System.out.println("\t" + key + " : " + value);

       }


System.out.println("###########################################");

}

}


# 참고 사이트

http://findnerd.com/list/view/Parse-Json-Object-with-dynamic-keys-using-Gson-/24094/

https://stackoverflow.com/questions/8829475/json-deserialization-of-dynamic-object-using-gson

https://futurestud.io/tutorials/gson-mapping-of-maps

https://www.codesd.com/item/creating-pojo-outside-the-json-string-with-dynamic-fields-using-gson.html

https://www.e-learn.cn/content/wangluowenzhang/34690

http://javausecase.com/2018/02/23/gson-how-to-parse-input-json-with-dynamic-keys/



'Java > Java' 카테고리의 다른 글

SimpleDateFormat: 현재날짜 구하기  (0) 2018.06.19
타이머: Timer, ScheduledExcutorService  (0) 2018.06.19
Map Collection이란?  (0) 2018.05.23
Set Collection이란?  (0) 2018.05.23
List Collection이란?  (0) 2018.05.23