//웹에서 UTF-8로 인코딩된 Data를 읽어옴
void GetJson() {
try {
String StrURL = "http://localhost/JsonServer/MyJson.jsp";
URL url = new URL(StrURL);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
InputStream in = conn.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in));
String imsi = "";
output = "";
while(true)
{
imsi = rd.readLine();
if(imsi == null)
break;
output += imsi;
}
ParseJson();
} catch (Exception e) {
e.printStackTrace();
}
}
//읽어온 Data를 Parsing
void ParseJson() {
try {
output = java.net.URLDecoder.decode(output, "UTF-8");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
JSONParser parse = new JSONParser();
JSONObject obj = null;
JSONArray arr = null;
try {
arr = (JSONArray)parse.parse(output);
for( int i = 0 ; i < arr.size(); i++)
{
obj = (JSONObject) arr.get(i);
System.out.println("이름 : " + obj.get("name"));
System.out.println("나이 : " + obj.get("age"));
}
} catch (ParseException e) {
e.printStackTrace();
}
}