본문 바로가기
JAVA

Array/Json 데이터 표현

by IT History 2023. 6. 28.
728x90
반응형

JSON Libraries 다운로드 사이트

https://mvnrepository.com/artifact/org.json/json

배열과 JSON 데이터 표현이 어떻게 이루어지는지 알아보자.

Array 및 Map을 활용하여 데이터 put 시 

Array / Map 데이터 표현
HashMap sendData = new HashMap<String, Object>();

HashMap mapTest = new HashMap();
ArrayList datsList = new ArrayList();
datsList.add("{REQUEST_ID,DATA_ID}");

mapTest.put("serviceId", "testID");
mapTest.put("LIST", datsList); 

sendData.put("requestBody", mapTest);

System.out.println(sendData);

/*Array 결과값*/
{requestBody={serviceId=testID, SOR_LIST=[{REQUEST_ID,DATA_ID}]}}

JSON 데이터 put 시

JSON 데이터 표현
JSONObject jsonMap = new JSONObject();
jsonMap.put("serviceId", "testID");

System.out.println(jsonMap);

JSONArray jsonArray = new JSONArray();
JSONObject jsonSorList = new JSONObject();
jsonSorList.put("LIST", "[{REQUEST_ID,DATA_ID}]");

jsonMap.put("requestBody", jsonSorList);

System.out.println(jsonMap);

/*JSON 결과값*/
{"requestBody":{"LIST":"[{REQUEST_ID,DATA_ID}]"},"serviceId":"testID"}
728x90
반응형

댓글