package json.test;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class TestOrgJSON {
public static void test1() throws JSONException{
JSONObject obj = new JSONObject();
obj.put("name", "홍길동");
obj.put("age", 20);
obj.put("tall", 190.7);
obj.put("male", true);
String str = obj.toString();
System.out.println("- test1() 결과 -");
System.out.println(str);//결과 : {"male":true,"age":20,"name":"홍길동","tall":190.7}
System.out.println();
}
public static void test2(){
//DTO를 이용해 JSONObject 생성
CustomerDTO cto = new CustomerDTO("id-111", "유재석", 20000, true);
JSONObject obj = new JSONObject(cto);
//String str = obj.toString();
//System.out.println(str);
System.out.println("- test2() 결과 -");
System.out.println(obj);//결과 : {"id":"id-111","mileage":20000,"name":"유재석","male":true}
System.out.println();
}
public static void test3()throws JSONException{
//{"mem": {"id":"id-111","mileage":20000,"name":"유재석","male":true}}
CustomerDTO cto1 = new CustomerDTO("id-111", "유재석", 20000, true);
CustomerDTO cto2 = new CustomerDTO("id-222", "홍길동", 20000, true);
CustomerDTO cto3 = new CustomerDTO("id-333", "박명수", 20000, true);
JSONObject ctoJSON1 = new JSONObject(cto1);
JSONObject ctoJSON2 = new JSONObject(cto2);
JSONObject ctoJSON3 = new JSONObject(cto3);
JSONObject obj = new JSONObject();
obj.put("cto1", ctoJSON1);
obj.put("cto2", ctoJSON2);
obj.put("cto3", ctoJSON3);
System.out.println("- test3() 결과 -");
System.out.println(obj);
System.out.println();
}
public static void test4() throws JSONException{
CustomerDTO cto1 = new CustomerDTO("id-111", "유재석", 20000, true);
CustomerDTO cto2 = new CustomerDTO("id-222", "홍길동", 30000, true);
CustomerDTO cto3 = new CustomerDTO("id-333", "박명수", 40000, true);
ArrayList<CustomerDTO> list = new ArrayList<CustomerDTO>();
list.add(cto1);
list.add(cto2);
list.add(cto3);
JSONObject obj = new JSONObject();
obj.put("customers", list);
System.out.println("- test4() 결과 -");
System.out.println(obj);
System.out.println();
}
//JSONArray이용 - 자바스크립트 배열형태의 문자열
public static void test5() throws JSONException{
JSONArray arr = new JSONArray();
arr.put("이순신");
arr.put(false);
arr.put(1000);
arr.put(20.48);
System.out.println("- test5() 결과 -");
System.out.println(arr);
System.out.println();
}
public static void test6() throws JSONException{
ArrayList list = new ArrayList();
list.add("홍길동");
list.add(true);
list.add(1000);
list.add(222.1);
JSONArray arr = new JSONArray(list);
System.out.println("- test6() 결과 -");
System.out.println(arr);
System.out.println();
}
public static void test7() throws JSONException{
ArrayList list = new ArrayList();
list.add("홍길동");
list.add(true);
list.add(1000);
list.add(222.1);
JSONArray arr = new JSONArray(list);
JSONObject obj = new JSONObject();
obj.put("list", arr);
System.out.println("- test7() 결과 -");
System.out.println(obj);
System.out.println(arr);
System.out.println();
}
public static void test8() throws JSONException{
String[] list = {"aaa","bbb","ccc"};
JSONArray arr = new JSONArray(list);
JSONObject obj = new JSONObject();
obj.put("list", arr);
System.out.println("- test8() 결과 -");
System.out.println(arr);
System.out.println(obj);
System.out.println();
}
public static void test9() throws JSONException{
CustomerDTO cto1 = new CustomerDTO("id-111", "유재석", 20000, true);
CustomerDTO cto2 = new CustomerDTO("id-222", "홍길동", 30000, true);
CustomerDTO cto3 = new CustomerDTO("id-333", "박명수", 40000, true);
JSONArray arr = new JSONArray();
arr.put(cto1);
arr.put(cto2);
arr.put(cto3);
System.out.println("- test9() 결과 -");
System.out.println(arr);
System.out.println();
}
public static void test10() throws JSONException{
CustomerDTO cto1 = new CustomerDTO("id-111", "유재석", 20000, true);
CustomerDTO cto2 = new CustomerDTO("id-222", "홍길동", 30000, true);
CustomerDTO cto3 = new CustomerDTO("id-333", "박명수", 40000, true);
ArrayList list = new ArrayList();
list.add(cto1);
list.add(cto2);
list.add(cto3);
JSONArray arr = new JSONArray(list);
System.out.println("- test10() 결과 -");
System.out.println(arr);
System.out.println();
}
public static void main(String[] args) throws JSONException{
test1();
test2();
test3();
test4();
test5();
test6();
test7();
test8();
test9();
test10();
}
}
package json.test;
public class CustomerDTO {
private String id;
private String name;
private int mileage;
private boolean male;
public CustomerDTO() {
super();
// TODO Auto-generated constructor stub
}
public CustomerDTO(String id, String name, int mileage, boolean male) {
super();
this.id = id;
this.name = name;
this.mileage = mileage;
this.male = male;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMileage() {
return mileage;
}
public void setMileage(int mileage) {
this.mileage = mileage;
}
public boolean isMale() {
return male;
}
public void setMale(boolean male) {
this.male = male;
}
@Override
public String toString() {
return "CustomerDTO [id=" + id + ", name=" + name + ", mileage="
+ mileage + ", male=" + male + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + (male ? 1231 : 1237);
result = prime * result + mileage;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
CustomerDTO other = (CustomerDTO) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (male != other.male)
return false;
if (mileage != other.mileage)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
* 결과
- test1() 결과 -
{"male":true,"age":20,"name":"홍길동","tall":190.7}
- test2() 결과 -
{"id":"id-111","mileage":20000,"name":"유재석","male":true}
- test3() 결과 -
{"cto1":{"id":"id-111","mileage":20000,"name":"유재석","male":true},"cto3":{"id":"id-333","mileage":20000,"name":"박명수","male":true},"cto2":{"id":"id-222","mileage":20000,"name":"홍길동","male":true}}
- test4() 결과 -
{"customers":[{"id":"id-111","mileage":20000,"name":"유재석","male":true},{"id":"id-222","mileage":30000,"name":"홍길동","male":true},{"id":"id-333","mileage":40000,"name":"박명수","male":true}]}
- test5() 결과 -
["이순신",false,1000,20.48]
- test6() 결과 -
["홍길동",true,1000,222.1]
- test7() 결과 -
{"list":["홍길동",true,1000,222.1]}
["홍길동",true,1000,222.1]
- test8() 결과 -
["aaa","bbb","ccc"]
{"list":["aaa","bbb","ccc"]}
- test9() 결과 -
["CustomerDTO [id=id-111, name=유재석, mileage=20000, male=true]","CustomerDTO [id=id-222, name=홍길동, mileage=30000, male=true]","CustomerDTO [id=id-333, name=박명수, mileage=40000, male=true]"]
- test10() 결과 -
[{"id":"id-111","mileage":20000,"name":"유재석","male":true},{"id":"id-222","mileage":30000,"name":"홍길동","male":true},{"id":"id-333","mileage":40000,"name":"박명수","male":true}]
'프로그래밍 > ajax' 카테고리의 다른 글
2012-5-31 ajax이용한 회원ID 전체 조회하기(eval사용)[실습] (0) | 2012.05.31 |
---|---|
json을 활용한 id조회하기(service,dto,controller사용)[실습] (0) | 2012.05.31 |
json을 이용한 테이블 관리 (0) | 2012.05.31 |
2012-5-31 AJAX와 서버 간의 데이터 송수신 기법(xml, json) (0) | 2012.05.31 |
2012-5-30 ajax를 활용한 지하철 노선도 검색[select]사용 (0) | 2012.05.30 |