Gson 학습

package com.bh.test;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

/**Gson test * @author cyq * */
public class Test {

    public Test() {
        method0();
    }

    /**           */
    public void method0(){

        Gson gson = new Gson();
        String json = gson.toJson(123);
        System.out.println(json);
        int target2 = gson.fromJson(json, int.class);
        System.out.println(target2);
    }
    /**                     */
    public void method1(){

        Type listType = new TypeToken<List<String>>() {}.getType();
        List<String> target = new LinkedList<String>();
        target.add("zs");

        Gson gson = new Gson();
        String json = gson.toJson(target, listType);
        System.out.println(json);
        List<String> target2 = gson.fromJson(json, listType);
        System.out.println(target2);
    }

    /** *          ,              ,         ,     null ,         json    ,  gson     。 *      gson     ,      null       json    。 */
    public void method2(){

        Gson gson = new GsonBuilder().setVersion(1.1).serializeNulls().setPrettyPrinting().create();//   serializeNulls  ,  gson       
        Gson gson1 = new Gson();
        Person person=new Person();
        person.setAge(10);
        person.setName("zs");
        String gsonStr=gson.toJson(person);
        System.out.println(gsonStr);
        System.out.println(gson.fromJson(gsonStr, Person.class));
        String gsonStr1=gson1.toJson(person);
        System.out.println(gsonStr1);
        System.out.println(gson1.fromJson(gsonStr1, Person.class));
    }

    /** *       ,           */
    public void method3(){

        Gson gson = new Gson();
        String str= "{\"name\":\"value1\",\"age\":\"11\"}";
        String str2= "{\"id\":\"11\",\"name\":\"value2\"}";
        List<String>list=new ArrayList<>();
        list.add(str);
        list.add(str2);
        String jsonStr=gson.toJson(list);
        System.out.println(jsonStr);
        List<String> obj = gson.fromJson(jsonStr, new TypeToken<List<String>>() {}.getType());
        System.out.println(gson.fromJson(obj.get(0), Person.class));
        System.out.println(gson.fromJson(obj.get(1), User.class));
    }

    /**        */
    public void method4(){

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();   
        User user=new User();  
        user.setName("lmw");  
        user.setBirthDay(new Date()); 
        String jsonStr=gson.toJson(user);
        System.out.println(jsonStr);  
        User uu=gson.fromJson(jsonStr, User.class);  
        System.out.println(uu);  
    }

    /**     */
    public void method5(){

        Gson gson = new GsonBuilder().registerTypeAdapter(Hobby.class,new EnumSerializer()).create();
        User user = new User();
        user.setName("item_name");
        user.setHobby(Hobby.BASKETBALL);//    state     
        String s = gson.toJson(user);
        System.out.println(s);
        User retItem = gson.fromJson(s, User.class);  
        System.out.println(retItem);
    }

    public static void main(String[] args) {
        new Test();
    }
}

좋은 웹페이지 즐겨찾기