Java에서 key, value 값을 Json Object담고 다시 jsonArray로 담아서 출력한다.
해당 부분은 그리드 화면에서 key, value 값을 비교 할 때 유용하다.
| Java에서 Json(Object, Array) 다루기 |
|
import java.util.HashMap;
import java.util.Map; import org.json.simple.JSONArray; import org.json.simple.JSONObject; public class Test { public static void main(String[] args) { Map<String, Object> result = new HashMap<String, Object>(); Map<String, String> item = new HashMap<String, String>(); item.put("1", "사과"); item.put("2", "딸기"); item.put("3", "배"); item.put("4", "토마토"); item.put("5", "망고"); JSONObject obj = new JSONObject(); JSONArray jsonArray = new JSONArray(); for(Map.Entry entry : item.entrySet()){ String key = (String) entry.getKey(); String value = (String) entry.getValue(); System.out.println(key+"|"+ value); obj.put(key, value); } jsonArray.add(obj); result.put("resultLIst : ", jsonArray); System.out.println(result); } } |
![]() |
'Java' 카테고리의 다른 글
| JAVA SOLID 개념 (0) | 2024.07.17 |
|---|---|
| JAVA 파일 읽기 및 효율적인 I/O 처리 방법 (0) | 2024.06.15 |
| String vs StringBuilder 속도 비교 (0) | 2024.03.23 |
| Java 개행처리 (0) | 2024.02.28 |
| Java JDBC Connection (0) | 2024.02.24 |
