본문 바로가기
백엔드/Spring

[spring] Json 데이터 Object, Array 값 key, value 가져오기

by 작은소행성 2021. 12. 14.

Json 데이터 Object, Array 값 key, value 가져오기

 

 

오픈 API 결과를 스트링 버퍼에 저장을 시켰다.

 

StringBuffer response = new StringBuffer();
                while ((inputLine = br.readLine()) != null) {
                    response.append(inputLine);
                }

 

 

 

 

다음과 같은 JSON 데이터를 키 값으로 나누어서 사용하고자 한다. 

 

{
   "result":[
      {
         "searchView":{
            "divFlag":"ARTICLE",
            "lodID":"4740152",
            "title":"기후변화 시나리오에 의한 외래식물 실망초(Conyza bonariensis)의 서식지 분포 예측",
            "author":[
               "이**",
               "오**",
               "홍**",
               "나**",
               "나**",
               "김**",
               "손**"
            ],
            "journalName":"한국기후변화학회지",
            "orgName":"한국기후변화학회",
            "pubYear":"2015",
            "uci":"G901:A-0003859654",
            "doi":null,
            "lang":"kor",
            "keyword":[
               "Invasive Alien Plant",
               "Conyza bonariensis",
               "Climate Change",
               "MaxEnt"
            ],
            "abst":"This study was conducted to predict the changes of potential distribution for invasive alien plant, Conyza bonariensis in Korea. C. bonariensis was found in southern Korea (Jeju, south coast, southwest coast). The habitats of C. bonariensis were roadside, bare ground, farm area, and pasture, where the interference by human was severe. Due to the seed characteristics of Compositae, C. bonariensis take long scattering distance and it will easily spread by movement of wind, vehicles and people. C. canadensis in same Conyza genus has already spread on a national scale and it is difficult to manage. We used maximum entropy modeling (MaxEnt) for analyzing the environmental influences on C.\nbonariensis distribution and projecting on two different RCP scenarios, RCP 4.5 and RCP 8.5. The results of our study indicated annual mean temperature, elevation and temperature seasonality had higher contribution for C. bonariensis potential distribution. Area under curve (AUC) values of the model was 0.9. Under future climate scenario, the constructed model predicted that potential distribution of C. bonariensis will be increased by 338% on RCP 4.5 and 769% on RCP 8.5 in 2100s.\n",
            "toc":null,
            "degreeType":null,
            "major":null,
            "advisor":null,
            "graduateSchool":null,
            "confermentYear":null,
            "docUrl":null
         }
      }
   ]
}

 

 

 

JsonObject는 중괄호의 속성을 정의한다.

{"result" : ["*******"] }

 

JsonArray는 대괄호의 속성을 정의한다.

[ {"result" : ["*******"] } ]

 

두 속성을 참고해서 필요한 데이터를 추출해서 사용하면 된다.

 		JSONParser parser = new JSONParser();
                JSONObject jsonObjects = (JSONObject) parser.parse(response.toString());
                System.out.println("jsonObjects > " + jsonObjects);

                JSONObject jobj1 = new JSONObject(jsonObjects);
                JSONArray jobj2 = (JSONArray) jobj1.get("result");
                System.out.println("jobj2 > " + jobj2+" , jobj2.size() > "+jobj2.size());


                for (int i = 0; i < jobj2.size(); i++) {
                    JSONObject jobj3 = (JSONObject) jobj2.get(i);
                    System.out.println("jobj3 > " + jobj3);

                    jobj3.get("searchView");
                    System.out.println("searchView > " + jobj3.get("searchView"));

                    JSONObject jobj4 = (JSONObject) jobj3.get("searchView");
                    System.out.println("jobj4 > " + jobj4);

                    String title = (String) jobj4.get("title");
                    String pubYear = (String) jobj4.get("pubYear");
                    String abst = (String) jobj4.get("abst");
                    System.out.println("title > " + title + " , pubYear > " + pubYear);
                    System.out.println("abst > " + abst);


                    JSONArray jobj7 = (JSONArray) jobj4.get("keyword");
                    System.out.println("jobj7 >> " + jobj7);

                }

 

 

반응형