JSON API 활용하기(JSON-Java)
미해결
Java TPC 실전프로젝트 (Java API 활용)
package TPC; import org.json.*; public class Project01_Bd { public static void main(String[] args ) { // JSON-Java(org.json) JSONArray students = new JSONArray(); //JSONArray 생성 JSONObject student = new JSONObject(); // JSONObject객체생성 student .put( "name" , "홍길동" ); // key와 value쌍으로 이루어진 구조인 put메서드를 사용해서 studunt에 데이터를 집어넣는다. student .put( "phone" , "010-1111-1111" ); student .put( "address" , "서울" ); //여기까지가 한사람의 JSON 객체다. System. out .println( student ); //{"address":"서울","phone":"010-1111-1111","name":"홍길동"} // 여기까지 우리가 JSON으로 굳이 객체를 만들지 않더라도 제이슨 구조가 바로 만들어진다. students .put( student ); System. out .println( student ); //{"address":"서울","phone":"010-1111-1111","name":"홍길동"} student = new JSONObject(); // 새로운 인스턴스 생성되어 변수에 할당되어 이전 인스턴스에 대한 참조가 대체 (홍길동 -> 나길동) student .put( "name" , "나길동" ); student .put( "phone" , "010-1111-1111" ); student .put( "address" , "서울" ); students .put( student ); //배열은 호출 시점에 객체에 대한 참조를 보유하므로 홍길동 -> 나길동으로 재할당 되었더라도 배열은 원래 객체에 대한 참조를 유지한다. System. out .println( student ); //{"address":"서울","phone":"010-1111-1111","name":"나길동"} System. out .println( students ); //[{"address":"서울","phone":"010-1111-1111","name":"홍길동"},{"address":"서울","phone":"010-1111-1111","name":"나길동"}] } } student라는 객체를 홍길동에서 나길동으로 재할당했는데, 배열은 호출시점에 객체에 대한 참조를 보유해서 재할당하더라도 배열은 이전 객체에 대한 참조를 유지한다고 하는데.. 그러면 홍길동은 배열에선 살아있는거고 (?) student 객체에서는 사라진건가요..?....... 그리고 JSONObject에서 put메서드가 key-value 쌍이라고 설명하셨는데, JSONArray는 왜 put메서드를 객체로 저장하나요? put메서드는 객체에 저장하는지 배열에 저장하는지에 따라 구조가 달라지는건가요?
- java
- 배열
- 객체
- put메서드





