1. 공공데이터 출력 (.csv)
package com.example.Ex02.TEST;
import java.io.*;
public class Ex01 {
public static void main(String[] args) {
File file =new File("file\\기상청27_관광코스별_관광지_지점정보.csv");
BufferedReader br;
String line;
int count =0;
try {
br = new BufferedReader(new FileReader(file)); //연결 다리
while((line = br.readLine()) != null) {
count++;
String [] arr = line.split(",");
// 남해, 힐링 관광지 출력
if (arr[4].contains("(남해)") && arr[10].contains("힐링")){
System.out.println(line);
}
}
System.out.println("Count :" + count);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
package com.example.Ex02.TEST;
import java.io.*;
public class Ex02 {
public static void main(String[] args) {
// 강남구 도서관 출력
File file =new File("file\\서울시 공공도서관 현황정보.csv");
BufferedReader br;
String line;
try {
br = new BufferedReader(new FileReader(file));
while((line = br.readLine()) != null){
String[]arr = line.split(",");
if(arr[3].equals("\"강남구\"")){
System.out.println(line);
}
}
br.close();
br = new BufferedReader(new FileReader(file));
// 어린이가 포함된 도서관 출력
int count=0;
while((line = br.readLine()) != null){
String[]arr = line.split(",");
if (arr[1].contains("어린이")) {
count++;
System.out.println(line);
}
}
System.out.println("총 어린이 도서관 개수: " + count);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
2. 공공데이터 출력 ( json)
pom.xml 입력
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
현재 위치 출력
public class Ex03 {
public static void main(String[] args) throws IOException, ParseException, JSONException {
Path currentPath = Paths.get("."); // 현재 위치
String path = currentPath.toAbsolutePath().toString();
System.out.println("path : " + path); // path : C:\Spring_study\Ex16\.
test1.json
{
"name" : "apple",
"id" : 1,
"price" : 1000
}
System.out.println("=========Ex01=========");
JSONParser parser = new JSONParser(); // Json (parser)파서기를 이용해 속성과 값을 분리
Reader reader = new FileReader("file\\test1.json");
JSONObject jsonObject = (JSONObject)parser.parse(reader);
String name = (String) jsonObject.get("name");
long id = (Long) jsonObject.get("id");
long price = (Long) jsonObject.get("price");
System.out.println("name :" + name);
System.out.println("id :" + id);
System.out.println("price :" + price);
Set keySet = jsonObject.keySet();
for (Object key : keySet) {
System.out.println("key: " + key + ", value: " + jsonObject.get(key));
}
}
test2.json
[
{"name":"길동","age":"44"},
{"name":"상철","age":"33"}
]
System.out.println("=========Ex02=========");
JSONParser parser2 = new JSONParser();
Reader reader2 = new FileReader("file\\test2.json");
Object jsonObject2 = parser2.parse(reader2);
JSONArray jsonArr2 = (JSONArray) jsonObject2;
System.out.println(jsonArr2.size());
for (int i = 0; i < jsonArr2.size(); i++) {
JSONObject jObject = (JSONObject)jsonArr2.get(i);
Set keySet2 = jObject.keySet();
for (Object key : keySet2) {
System.out.println(key + " : " + jObject.get(key));
}
}
test3.json
{
"id" : 1,
"employee" : [
{"name" : "윤아", "age":30 },
{"name" : "웬디", "age":50 }
],
"company" :"메타 빌드"
}
System.out.println("=========Ex03=========");
JSONParser parser3 = new JSONParser();
Reader reader3 = new FileReader("file\\test3.json");
JSONObject jsonObject3 = (JSONObject)parser3.parse(reader3);
JSONArray jsonArr3 = (JSONArray)jsonObject3.get("employee");
for (int i = 0; i < jsonArr3.size(); i++){
JSONObject jObject3 = (JSONObject)jsonArr3.get(i);
Set keySet3 = jObject3.keySet();
for (Object key : keySet3){
System.out.println(key +":"+jObject3.get(key));
}
}
💡 요약 한 줄
Ex01 = {} 단일 객체
Ex02 = [] 여러 객체 배열
Ex03 = { "키": [ ... ] } 객체 내부 배열
| 파일명 | JSON 구조 | 파싱 객체 | 접근 방법 |
| test1.json | {} 단일 객체 | JSONObject | .get("키") |
| test2.json | [] 배열 | JSONArray | for + get(i) |
| test3.json | { "employee": [ ... ] } | JSONObject → JSONArray | get("employee") 후 반복 |
전체코드
더보기
package com.example.Ex02.TEST;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
public class Ex03 {
public static void main(String[] args) throws IOException, ParseException {
// 현재 실행 경로 출력
Path currentPath = Paths.get(".");
String path = currentPath.toAbsolutePath().toString();
System.out.println("path : " + path);
// ------------------ Ex01: 단일 객체 ------------------
System.out.println("=========Ex01=========");
JSONParser parser = new JSONParser();
Reader reader = new FileReader("file\\test1.json");
JSONObject jsonObject = (JSONObject) parser.parse(reader);
// 개별 key-value 접근
System.out.println("name : " + jsonObject.get("name"));
System.out.println("id : " + jsonObject.get("id"));
System.out.println("price : " + jsonObject.get("price"));
// 전체 key-value 출력
for (Object key : jsonObject.keySet())
System.out.println(key + " : " + jsonObject.get(key));
// ------------------ Ex02: 배열 ------------------
System.out.println("=========Ex02=========");
JSONParser parser2 = new JSONParser();
Reader reader2 = new FileReader("file\\test2.json");
JSONArray jsonArr2 = (JSONArray) parser2.parse(reader2);
// 배열 안의 각 객체 출력
for (Object obj : jsonArr2) {
JSONObject jObject = (JSONObject) obj;
for (Object key : jObject.keySet())
System.out.println(key + " : " + jObject.get(key));
}
// ------------------ Ex03: 객체 내부 배열 ------------------
System.out.println("=========Ex03=========");
JSONParser parser3 = new JSONParser();
Reader reader3 = new FileReader("file\\test3.json");
JSONObject jsonObject3 = (JSONObject) parser3.parse(reader3);
// "employee" 배열 추출 후 반복 출력
JSONArray jsonArr3 = (JSONArray) jsonObject3.get("employee");
for (Object obj : jsonArr3) {
JSONObject jObject3 = (JSONObject) obj;
for (Object key : jObject3.keySet())
System.out.println(key + " : " + jObject3.get(key));
}
}
}
3. 공공데이터 출력 ( json)
'기초 및 언어 > ▶ Spring' 카테고리의 다른 글
| 20. Spring_Swagger (0) | 2025.12.03 |
|---|---|
| 19. Spring_SLF4J Logger (0) | 2025.12.03 |
| 17. Spring_BookSite만들기-1 (1) | 2025.10.14 |
| 16. Spring_Thymeleaf 헤더(Fragment)로 로그인 상태 표시하기 (0) | 2025.10.10 |
| 15. Spring Boot JPA + 쿼리 어노테이션(jpql) (0) | 2025.10.01 |