내용

글번호 447
작성자 heojk
작성일 2017-02-12 12:24:36
제목 GSON 라이브러리를 이용한 JSON으로 응답하고 객체로 변환해 처리하기
내용 JSON to Java Object 자바 라이브러리를 이용한 JSON으로 응답하고 객체로 변환해 처리하기 참고 주소 : https://code.google.com/p/google-gson/ - Maven dependency
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.3.1</version>
</dependency>
1. Primitive example (Serialization)
Gson gson = new Gson();
gson.toJson(1);            ==> prints 1
gson.toJson("abcd");       ==> prints "abcd"
gson.toJson(new Long(10)); ==> prints 10
int[] values = { 1 };
gson.toJson(values);       ==> prints [1]
(Deserialization)
int one = gson.fromJson("1", int.class);
Integer one = gson.fromJson("1", Integer.class);
Long one = gson.fromJson("1", Long.class);
Boolean false = gson.fromJson("false", Boolean.class);
String str = gson.fromJson("\"abc\"", String.class);
String anotherStr = gson.fromJson("[\"abc\"]", String.class);
2. Object Example
class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}
(Serialization)
BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
==> json is {"value1":1,"value2":"abc"}
Note that you can not serialize objects with circular references since that will result in infinite recursion.  (Deserialization)
BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
==> obj2 is just like obj
3. ArrayExample
Gson gson = new Gson();
int[] ints = {1, 2, 3, 4, 5};
String[] strings = {"abc", "def", "ghi"};
(Serialization)
gson.toJson(ints);     ==> prints [1,2,3,4,5]
gson.toJson(strings);  ==> prints ["abc", "def", "ghi"]
(Deserialization)
int[] ints2 = gson.fromJson("[1,2,3,4,5]", int[].class); 
==> ints2 will be same as ints
We also support multi-dimensional arrays, with arbitrarily complex element types 4. Collections Example
Gson gson = new Gson();
Collection<Integer> ints = Lists.immutableList(1,2,3,4,5);
(Serialization)
String json = gson.toJson(ints); ==> json is [1,2,3,4,5]
(Deserialization)
java.lang.reflect.Type collectionType = new TypeToken<Collection<Integer>>(){}.getType();
Collection<Integer> ints2 = gson.fromJson(json, collectionType);
ints2 is same as ints
Fairly hideous: note how we define the type of collection Unfortunately, no way to get around this in Java Collections Limitations Can serialize collection of arbitrary objects but can not deserialize from it Because there is no way for the user to indicate the type of the resulting object While deserializing, Collection must be of a specific generic type All of this makes sense, and is rarely a problem when following good Java coding practices 5. JSON 데이터를 객체로 변환
import java.lang.reflect.Type;
import com.google.gson.reflect.TypeToken;

java.lang.reflect.Type collectionType = new TypeToken<List<ProductVO>>(){}.getType();
List<ProductVO> lists = gson.fromJson(jsonStr, collectionType);
System.out.println(lists);
첨부파일 JSON으로 응답하고 객체로 변환해 처리하기.pdf (431,118byte)