본문 바로가기

New Spring

JSON 자바 출력 (json-simple)

 

<dependency>

<groupId>com.googlecode.json-simple</groupId>

<artifactId>json-simple</artifactId>

<version>1.1.1</version>

</dependency>

 

 

1. JSONObject 객체생성

JSONObject jsonObj = new JSONObject();

jsonObj.put("userId", "hong");

jsonObj.put("userName", "홍길동");

 

// json 객체에서 value 값 가져오기

String userId = (String) jsonObj.get("userId");

String userName = (String) jsonObj.get("userName");

System.out.println("userId:" + userId);

System.out.println("userName:" + userName);

 

// JSONObject 를 String으로 변환

String jsonStr = jsonObj.toJSONString();

 

// String을 JSONObject로 변환

JSONParser parser = new JSONParser();

JSONObject obj = (JSONObject) parser.parse(jsonStr);

System.out.println("obj:" + obj);

System.out.println("userId:" + obj.get("userId"));

System.out.println("userName:" + obj.get("userName"));

 

 

 

 

2. JSONArray 배열 생성

JSONObject obj1 = new JSONObject();

obj1.put("userId", "kang");

obj1.put("userName", "강호동");

 

JSONObject obj2 = new JSONObject();

obj2.put("userId", "hans");

obj2.put("userName", "한효주");

 

JSONArray list = new JSONArray();

list.add(obj1);

list.add(obj2);

 

System.out.println("list:" + list);

 

for(int i=0; i<list.size(); i++) {

Map<String, Object> getObj = (HashMap<String, Object>) list.get(i);

String userId = (String) getObj.get("userId");

String userName = (String) getObj.get("userName");

System.out.println("userId:" + userId);

System.out.println("userName:" + userName);

}

 

 

3. Map을 JSONObject로

 

Map<String, Object> map = new HashMap<>();

map.put("userId", "park");

map.put("userName", "박찬호");

System.out.println("map:" + map);

 

JSONObject json = new JSONObject(map);

System.out.println("json:" + json);

 

// JSONObject를 String로

String jsonStr = json.toString();

System.out.println("jsonStr:" + jsonStr);

 

//  String을 JSONObject로

JSONParser parser = new JSONParser();

JSONObject jsonObj = (JSONObject) parser.parse(jsonStr);

System.out.println("jsonObj:" + jsonObj);

 

 

 

 

 

// List<Map<String, Object>> 를 Json 으로 변환

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

 

Map<String, Object> map1 = new HashMap<>();

map1.put("userId", "park");

map1.put("userName", "박찬호");

System.out.println("map1:" + map1);

list.add(map1);

 

Map<String, Object> map2 = new HashMap<>();

map2.put("userId", "lees");

map2.put("userName", "이승엽");

System.out.println("map2:" + map2);

list.add(map2);

 

Map<String, Object> map3 = new HashMap<>();

map3.put("userId", "hans");

map3.put("userName", "한효주");

System.out.println("map3:" + map2);

list.add(map3);

 

System.out.println("list:" + list);

 

String listStr = list.toString();

System.out.println("listStr:" + listStr);

 

 

 

 

// 방법1 : List<Map<String, Object>> 를 Json 으로 변환

JSONArray jsonArr = new JSONArray();

 

for(int i=0; i<list.size(); i++) {

JSONObject jsonObj = new JSONObject();

System.out.println("list:" + list.get(i));

String userId = (String) list.get(i).get("userId");

String userName = (String) list.get(i).get("userName");

System.out.println("userId:" + userId);

System.out.println("userName:" + userName);

jsonObj.put("userId", userId);

jsonObj.put("userName", userName);

jsonArr.add(jsonObj);

}

System.out.println("jsonArr:" + jsonArr);

 

 

 

// 방법2 : List<Map<String, Object>> 를 Json 으로 변환
JSONArray jsonArr = new JSONArray();

for(int i=0; i<list.size(); i++) {

   JSONObject jsonObj = new JSONObject();

   Map<String, Object> user = list.get(i);

   System.out.println("user:" + user);

   Set<Map.Entry<String, Object>> entries = user.entrySet();

   for(Map.Entry<String, Object> entry : entries) {

      jsonObj.put(entry.getKey(), entry.getValue());

   }

   jsonArr.add(jsonObj);

}

System.out.println("jsonArr:" + jsonArr);

 

 

 

// 참고

 

String jsonStr =

"{"

+ "\"info1\": {"

+ "\"title\": \"json 예제1\","

+ "\"url\": \"https://naver.com/\","

+ "\"jsp\": false"

+" },"

+ "\"info1\": {"

+ "\"title\": \"json 예제2\","

+ "\"url\": \"https://daum.net/\","

+ "\"jsp\": false"

+ "}"

+"}";

 

// 가장 큰 JSONObject를 가져옵니다.

JSONObject jsonObj = new JSONObject();

JSONParser parser = new JSONParser();

JSONObject obj = (JSONObject) parser.parse(jsonStr);

System.out.println("obj:" + obj);

 

Map<String, Object> info1 = (Map<String, Object>) obj.get("info1");

System.out.println("info1:" + info1);

 

String title = (String) info1.get("title");

String url = (String) info1.get("url");

Boolean jsp = (Boolean) info1.get("jsp");

System.out.println("title:" + title);

System.out.println("url:" + url);

System.out.println("jsp:" + jsp);

 

 

 

 

'New Spring' 카테고리의 다른 글

java RESTAPI  (0) 2021.11.17
ajax json 전송  (0) 2020.09.16
JSON 자바출력 (jackson)  (0) 2020.09.02