웹개발지식쌓기
[back] Java JSON 2뎁스 이상 구조 만들기
developer_j
2022. 11. 30. 17:59
728x90
반응형
api로 데이터를 보내기 위해 json구조를 java에서 만들어야 했음
java 단에서 JSON object 를 만들 수 있는 방법은 여러가지인데 JSONObject.put() 메소드를 이용해보았다.
String으로 String json = "{ \"depth1\" : { .... 이런식으로 짜는 것보다 좋은 듯
중간에 if로 분기문도 넣을 수 있음
import org.json.JSONObject;
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
JSONObject jsonObject2 = new JSONObject();
jsonObject2.put("depth2", "true");
jsonObject.put("depth1", jsonObject2);
System.out.println(jsonObject.toString());
}
{"depth1":{"depth2":"true"}}
다만 뎁스가 너무 깊어지거나 깊은 뎁스인데 데이터는 몇개 안들어가면 하드코딩이나 다름없...
728x90
반응형