<%
/*
Object[][] sampleArr = {
{"초아", "AOA","걸그룹",25}
,{"전효성","시크릿","가수",26}
,{"정은지","에이핑크","탤런트",22}
,{"배수지","미스에이","영화배우",21}
};
JSONObject jsonList = new JSONObject();
JSONArray itemList = new JSONArray();
for(int i = 0; i < sampleArr.length;i++){
JSONObject tempJson = new JSONObject();
tempJson.put("name", sampleArr[i][0]);
tempJson.put("group", sampleArr[i][1]);
tempJson.put("job", sampleArr[i][2]);
tempJson.put("age", sampleArr[i][3]);
itemList.add(tempJson);
}
jsonList.put("TOTAL",sampleArr.length);
jsonList.put("ITEMS", itemList);
out.print("<script> alert('" + jsonList + "'); </script>");
*/
//JSON 데이터
String jsonInfo = "{\"books\":[{\"genre\":\"소설\",\"price\":\"100\",\"name\":\"사람은 무엇으로 사는가?\",\"writer\":\"톨스토이\",\"publisher\":\"톨스토이 출판사\"},{\"genre\":\"소설\",\"price\":\"300\",\"name\":\"홍길동전\",\"writer\":\"허균\",\"publisher\":\"허균 출판사\"},{\"genre\":\"소설\",\"price\":\"900\",\"name\":\"레미제라블\",\"writer\":\"빅토르 위고\",\"publisher\":\"빅토르 위고 출판사\"}],\"persons\":[{\"nickname\":\"남궁민수\",\"age\":\"25\",\"name\":\"송강호\",\"gender\":\"남자\"},{\"nickname\":\"예니콜\",\"age\":\"21\",\"name\":\"전지현\",\"gender\":\"여자\"}]}";
/*
{
"books": [
{
"genre": "소설",
"price": "100",
"name": "사람은 무엇으로 사는가?",
"writer": "톨스토이",
"publisher": "톨스토이 출판사"
},
{
"genre": "소설",
"price": "300",
"name": "홍길동전",
"writer": "허균",
"publisher": "허균 출판사"
},
{
"genre": "소설",
"price": "900",
"name": "레미제라블",
"writer": "빅토르 위고",
"publisher": "빅토르 위고 출판사"
}
],
"persons": [
{
"nickname": "남궁민수",
"age": "25",
"name": "송강호",
"gender": "남자"
},
{
"nickname": "예니콜",
"age": "21",
"name": "전지현",
"gender": "여자"
}
]
}
*/
try {
JSONParser jsonParser = new JSONParser();
//JSON데이터를 넣어 JSON Object 로 만들어 준다.
JSONObject jsonObject = (JSONObject) jsonParser.parse(jsonInfo);
//books의 배열을 추출
JSONArray bookInfoArray = (JSONArray) jsonObject.get("books");
System.out.println("* BOOKS *");
for(int i=0; i<bookInfoArray.size(); i++){
System.out.println("=BOOK_"+i+" ===========================================");
//배열 안에 있는것도 JSON형식 이기 때문에 JSON Object 로 추출
JSONObject bookObject = (JSONObject) bookInfoArray.get(i);
//JSON name으로 추출
System.out.println("bookInfo: name==>"+bookObject.get("name"));
System.out.println("bookInfo: writer==>"+bookObject.get("writer"));
System.out.println("bookInfo: price==>"+bookObject.get("price"));
System.out.println("bookInfo: genre==>"+bookObject.get("genre"));
System.out.println("bookInfo: publisher==>"+bookObject.get("publisher"));
}
JSONArray personInfoArray = (JSONArray) jsonObject.get("persons");
System.out.println("\r\n* PERSONS *");
for(int i=0; i<personInfoArray.size(); i++){
System.out.println("=PERSON_"+i+" ===========================================");
JSONObject personObject = (JSONObject) personInfoArray.get(i);
out.print("<script> alert('" + personObject.get("name") + "'); </script>");
System.out.println("personInfo: name==>"+personObject.get("name"));
System.out.println("personInfo: age==>"+personObject.get("age"));
System.out.println("personInfo: gender==>"+personObject.get("gender"));
System.out.println("personInfo: nickname==>"+personObject.get("nickname"));
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
%>
<%
try{
//URLConnection을 이용한 스트림 생성
URL url = new URL("http","10.1.97.1", 8080, "/assets/new");
URLConnection conn = (URLConnection) url.openConnection();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is,"utf-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
StringBuilder sb = new StringBuilder(1024);
while ((line=br.readLine()) != null){ // 라인단위 읽기
sb.append(line+"\n");
}
//out.println(sb.toString());
out.print("<script> alert('" + sb.toString() + "'); </script>");
}catch (IOException e) {
out.println(e.toString()); //에러 발생시 메시지 출력
}
%>