JSON - JAVA
JSON - JAVA
์์ด ๋ค๋ค๋ JSON์ FrameWork Library๋ฅผ ํตํด JAVA์ ์ ์ฉํด ๋ณผ ์ ์๋ค.
์ฝ๊ฒ๋งํด JSON DATA๋ฅผ JAVA domain ๊ฐ์ฒด๋ก domain ๊ฐ์ฒด๋ฅผ JSON DATA๋ก ์ฝ๊ฒ ๋ฐ๊ฟ ์ค ์ ์๋ค.
๋จผ์ JSON์ ์ฌ์ฉํ๋ ค๋ฉด JSON์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ฐ์์ผํ๋ค.
JSON-Simple Library๋ฅผ ๋ฐ์ lib๋ฅผ ๋ฑ๋กํ๋ฉด 2๊ฐ์ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํด๋์ค๋ฅผ ์ฌ์ฉ ํ ์ ์๊ฒ ๋๋ค. ํ์ง๋ง JSON-Simple์ ๋ฐ์ธ๋ฉ์ ํด์ฃผ์ง ์๋๋ค๋ ๋จ์ ์ ๊ฐ์ง๊ณ ์๋ค.
์ด๋ฌํ ๋ฐ์ธ๋ฉ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํด์ค CodeHaus ๋ผ๋ FrameWork๊ฐ ์๋ค.
์ด 2๊ฐ์ง FrameWork๋ฅผ ์์๋ณด์
JSON-Simple
ํฌ๊ฒ 2๊ฐ์ง ๋ก ๋๋ ์ ์๋๋ฐ JAVA DATA => JSON DATA์ผ๋ก ํ์ฑํ๋ JSONObject์ JSONArray๊ฐ ์๊ณ
JSON DATA๋ฅผ JAVA DATA ๋ก ํ์ฑํ๋ JSONValue๊ฐ ์๋ค.
JSONObject
Map๊ณผ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ฉฐ ๊ธฐ๋ณธ์ ์ผ๋ก put๊ณผ get์ ์ฌ์ฉํ๋ฉฐ {nama : value} ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ค.
JSONObject jsonObj = new JSONObject();
jsonObj.put("name", "ํ๊ธธ๋");
jsonObj.put("age", new Integer(1000));
jsonObj.put("address", "์์ธ");
// {"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋"}
System.out.println("JSON Object ํ์ธ : "+jsonObj);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonObj.get("address"));
/* ๊ฒฐ๊ณผ๊ฐ
JSON Object ํ์ธ : {"address":"์์ธ","name":"ํ๊ธธ๋","age":1000}
==>JSON Object Date ์ถ์ถ
์์ธ
*/
JSON์ String ๋ฐ์ดํฐ ๋ฟ๋ง ์๋๋ผ Map , List , Array ๋ฅผ ๋ฃ์ ์ ์๋ค.
JSONObject innerJsonObj = new JSONObject();
innerJsonObj.put("name", "ํ๊ธธ๋");
innerJsonObj.put("age", new Integer(1000));
innerJsonObj.put("address", "์์ธ");
jsonObj.put("innerJson", innerJsonObj);
// {"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋",
// "innerJson":{"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋"}}
System.out.println("JSON Object ํ์ธ : "+jsonObj);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonObj.get("address"));
JSONObject returnJsonObj = (JSONObject)jsonObj.get("innerJson");
System.out.println(returnJsonObj.get("address"));
/* ๊ฒฐ๊ณผ๊ฐ
JSON Object ํ์ธ : {"address":"์์ธ","name":"ํ๊ธธ๋",
"innerJson":{"address":"์์ธ","name":"ํ๊ธธ๋","age":1000},"age":1000}
==>JSON Object Date ์ถ์ถ
์์ธ
์์ธ
*/
Map <String, String> map = new HashMap<String, String>();
map.put("์ง๊ธ1", "์ฌ์");
map.put("์ง๊ธ2", "๋๋ฆฌ");
map.put("์ง๊ธ3", "๊ณผ์ฅ");
jsonObj.put("career", map);
// {"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋",
// "innerJson":{"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋"},
// {"career":{"์ง๊ธ2":"๋๋ฆฌ","์ง๊ธ3":"๊ณผ์ฅ","์ง๊ธ1":"์ฌ์"}}
System.out.println("JSON Object ํ์ธ : "+jsonObj);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonObj.get("career"));
Map returnMap = (Map)jsonObj.get("career");
System.out.println(returnMap.get("์ง๊ธ1"));
/* ๊ฒฐ๊ณผ๊ฐ
JSON Object ํ์ธ : {"career":{"์ง๊ธ2":"๋๋ฆฌ","์ง๊ธ3":"๊ณผ์ฅ","์ง๊ธ1":"์ฌ์"},
"address":"์์ธ","name":"ํ๊ธธ๋",
"innerJson":{"address":"์์ธ","name":"ํ๊ธธ๋","age":1000},"age":1000}
==>JSON Object Date ์ถ์ถ
{์ง๊ธ2=๋๋ฆฌ, ์ง๊ธ3=๊ณผ์ฅ, ์ง๊ธ1=์ฌ์}
์ฌ์
*/
JSONArray
์ด๋ฆ ๊ทธ๋๋ก Array์ ๊ฐ์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๋ฉฐ [value, value]์ ๊ตฌ์กฐ์ด๋ฉฐ add ์ remove๋ฅผ ์ฌ์ฉ ํ ์ ์๋ค.
JSONArray jsonArray = new JSONArray();
jsonArray.add(new Integer(1));
jsonArray.add("2");
jsonArray.add("3");
jsonObj.put("number", jsonArray);
//Data ์
๋ ฅ (CSV) {"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋",
// "innerJson":{"address":"์์ธ","age":1000,"name":"ํ๊ธธ๋"},
// {"career":{"์ง๊ธ2":"๋๋ฆฌ","์ง๊ธ3":"๊ณผ์ฅ","์ง๊ธ1":"์ฌ์"}, "number":[1,"2","3"]}
System.out.println("JSON Object ํ์ธ : "+jsonObj);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonObj.get("number"));
JSONArray returnJsonArray = (JSONArray)jsonObj.get("number");
System.out.println(returnJsonArray.get(0));
/* ๊ฒฐ๊ณผ๊ฐ
JSON Object ํ์ธ : {"number":[1,"2","3"],
"career":{"์ง๊ธ2":"๋๋ฆฌ","์ง๊ธ3":"๊ณผ์ฅ","์ง๊ธ1":"์ฌ์"},
"address":"์์ธ","name":"ํ๊ธธ๋",
"innerJson":{"address":"์์ธ","name":"ํ๊ธธ๋","age":1000},"age":1000}
==>JSON Object Date ์ถ์ถ
[1,"2","3"]
1
*/
JSONValue
JSON์ผ๋ก ๋ค์ด์จ DATA๋ฅผ JAVA์์ ์ฌ์ฉํ๊ธฐ ์ฝ๊ฒ ํ์ฑํด์ค๋ค.
//JSONValue
String data = "{\"address\":\"์์ธ\",\"age\":1000,\"name\":\"ํ๊ธธ๋\"}";
JSONObject jsonObj = (JSONObject)JSONValue.parse(data);
System.out.println("JSON Object ํ์ธ : "+jsonObj);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonObj.get("address"));
System.out.println("\n\n");
/* ๊ฒฐ๊ณผ๊ฐ
JSON Object ํ์ธ : {"address":"์์ธ","name":"ํ๊ธธ๋","age":1000}
==>JSON Object Date ์ถ์ถ
์์ธ
*/
/////////////////////////////////
//JSONValue Array
String arrayData = "[\"์์ธ\",1000,\"ํ๊ธธ๋\"]";
JSONArray jsonArray = (JSONArray)JSONValue.parse(arrayData);
System.out.println("JSON Object ํ์ธ : "+jsonArray);
System.out.println("==>JSON Object Date ์ถ์ถ");
System.out.println(jsonArray.get(0));
/*
JSON Object ํ์ธ : ["์์ธ",1000,"ํ๊ธธ๋"]
==>JSON Object Date ์ถ์ถ
์์ธ
*/
CodeHaus
JSON-Simple๊ณผ ๋ฌ๋ฆฌ domain๊ฐ์ฒด์์ ์๋ฐฉํฅ ๋ฐ์ธ๋ฉ์ ์ง์ํ๋ 3rd party Library์ด๋ค.
์๋ฐฉํฅ ๋ฐ์ธ๋ฉ์ ์ง์ํ๋ ObjectMapper์ ํ์ ์ ํ๋ณํ์ ํด์ฃผ๋ TypeReference๊ฐ ์๋ค.
CodeHaus๋ ๊ณต์ํ์ด์ง๊ฐ ์ฌ๋ผ์ ธ ํ์ฌ๋ Maven์ ํตํด์ ๋ค์ด๋ฐ์ ์ ์๋ค.
OjectMapper
ObjectMapper์๋ Domain => JSON์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ writeValueAsString()์
JSON => Domain ์ผ๋ก ๋ฐ์ธ๋ฉํด์ฃผ๋ readValue()๊ฐ ์๋ค.
User user = new User("user01", "ํ๊ธธ๋", "1111", null, 10);
ObjectMapper objectMapper = new ObjectMapper();
System.out.println("\n\n///////////////////////////////////////");
System.out.println("1.Domain Object => JSON Value(String) ๋ก ๋ณํ ");
String jsonOneValue = objectMapper.writeValueAsString(user);
System.out.println(jsonOneValue);
/* ๊ฒฐ๊ณผ๊ฐ
1.Domain Object => JSON Value(String) ๋ก ๋ณํ
{"userId":"user01","userName":"ํ๊ธธ๋","password":"1111","age":null,"grade":10,"regDate":null,"active":false}
*/
System.out.println("1.JSON Value => Domain Object ๋ณํ ๋ฐ ๊ฐ ์ถ์ถ");
User returnUser = objectMapper.readValue(jsonOneValue, User.class); // JSON Value์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ์ domain๊ฐ์ฒด
System.out.println(returnUser);
System.out.println("userId : "+returnUser.getUserId());
TypeReference
domain๊ฐ์ฒด ์ฌ๋ฌ๊ฐ๋ฅผ list์ ๋ฃ์ด JSON ์ผ๋ก ๋ณํํ๊ณ ๋ค์ ๊ฐ๊ฐ domain์ ๋ด์ list๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
domain๊ฐ์ฒด์ ๋ฌ๋ฆฌ List , Map๊ตฌ์กฐ๋ ํ๋ณํ ๊ณผ์ ์ ๊ฑฐ์ณ์ผํ๋๋ฐ new TypeReference<Generic>()๋ฅผ ํตํด ์ฝ๊ฒ ํ๋ณํ์ด ๊ฐ๋ฅํ๋ค.
List<User> list = new ArrayList<User>(10);
list.add(user);
list.add(new User("user02","ํ๊ธธ๋", "2222", null, 20));
System.out.println("2. List<User> => JSON Value(String) ๋ก ๋ณํ");
String jsonManyValue =objectMapper.writeValueAsString(list);
System.out.println(jsonManyValue);
/* ๊ฒฐ๊ณผ๊ฐ
2. List<User> => JSON Value(String) ๋ก ๋ณํ
[{"userId":"user01","userName":"ํ๊ธธ๋","password":"1111","age":null,"grade":10,"regDate":null,"active":false},{"userId":"user02","userName":"ํ๊ธธ๋","password":"2222","age":null,"grade":20,"regDate":null,"active":false}]
*/
System.out.println("2. JSON Value(String) => List<User> ๋ณํ ๋ฐ ๊ฐ ์ถ์ถ");
List <User> returnList = objectMapper.readValue(jsonManyValue, new TypeReference<List<User>>(){});
System.out.println(returnList);
System.out.println(returnList.get(0));
System.out.println("userId : "+returnList.get(0).getUserId());
/* ๊ฒฐ๊ณผ๊ฐ
2. JSON Value(String) => List<User> ๋ณํ ๋ฐ ๊ฐ ์ถ์ถ
[User [userid=user01, userName=ํ๊ธธ๋, password=1111, age=null, grade=10, active=false, regDate=null], User [userid=user02, userName=ํ๊ธธ๋, password=2222, age=null, grade=20, active=false, regDate=null]]
User [userid=user01, userName=ํ๊ธธ๋, password=1111, age=null, grade=10, active=false, regDate=null]
userId : user01
*/
Leave a comment