투케이2K

528. (Android/Java) [jackson 라이브러리] - JSON 전체 key 리스트 확인 실시 (json get all Key list) 본문

Android

528. (Android/Java) [jackson 라이브러리] - JSON 전체 key 리스트 확인 실시 (json get all Key list)

투케이2K 2023. 4. 2. 11:43

[개발 환경 설정]

개발 툴 : AndroidStudio

 

[소스 코드]

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

            /**
             * -------------------------------------
             * TODO [요약 설명]
             * -------------------------------------
             * 1. Jackson : Java Object 를 JSON으로 변환하거나 JSON 을 Java Object 로 변환하는데 사용할 수 있는 Java 라이브러리입니다
             * -------------------------------------
             * 2. 필요 라이브러리 설치 :
             *
             * implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4'
             * implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
             * implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4'
             * -------------------------------------
             * 3. 참고 사이트 :
             *
             * https://github.com/FasterXML/jackson
             *
             * https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core
             * -------------------------------------
             * */


            // [샘플 JSON 선언 실시]
            String json = "{\n" +
                    "   \"Name\":\"Craig\",\n" +
                    "   \"Age\":10,\n" +
                    "   \"BookInterests\":[\n" +
                    "      {\n" +
                    "         \"Book\":\"The Kite Runner\",\n" +
                    "         \"Author\":\"Khaled Hosseini\"\n" +
                    "      },\n" +
                    "      {\n" +
                    "         \"Book\":\"Harry Potter\",\n" +
                    "         \"Author\":\"J. K. Rowling\"\n" +
                    "      }\n" +
                    "   ],\n" +
                    "   \"FoodInterests\":{\n" +
                    "      \"Breakfast\":[\n" +
                    "         {\n" +
                    "            \"Bread\":\"Whole wheat\",\n" +
                    "            \"Beverage\":\"Fruit juice\"\n" +
                    "         },\n" +
                    "         {\n" +
                    "            \"Sandwich\":\"Vegetable Sandwich\",\n" +
                    "            \"Beverage\":\"Coffee\"\n" +
                    "         }\n" +
                    "      ]\n" +
                    "   }\n" +
                    "}";


            // [JSON get all Key list : JSON 전체 key 값 확인 실시]
            List<String> keys = new ArrayList<>();
            ObjectMapper mapper = new ObjectMapper();

            JsonNode jsonNode = mapper.readTree(json);
            JsonParser jsonParser = jsonNode.traverse();
            while (!jsonParser.isClosed()) {
                if (jsonParser.nextToken() == JsonToken.FIELD_NAME) {
                    keys.add((jsonParser.getCurrentName()));
                }
            }

            
            // [로그 출력 실시]
            S_Log.ltd("================================================");
            S_Log.cnt("["+ACTIVITY_NAME+" >> "+C_Util.getNowMethod(1)+" :: 로그 출력 실시]");
            S_Log.cnt("-------------------------------------------");
            S_Log.cnt("[key list :: "+keys.toString()+"]");
            S_Log.lbd("================================================");

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

[결과 출력]

 

D///===========//: ================================================
I/: [A_Intro >> onCreate :: 로그 출력 실시]
I/: -------------------------------------------
I/: [key list :: [Name, Age, BookInterests, Book, Author, Book, Author, FoodInterests, Breakfast, Bread, Beverage, Sandwich, Beverage]]
D///===========//: ================================================

 

반응형
Comments