Java JSON data operation (2) - Gson operation data JSON

Gson is Google released an open source Java library that can be used to convert Java objects to JSON string can also be used to JSON string into corresponding Java objects. Gson basic use, including serialization and deserialization of the present description; used herein to software version: Java 1.8.0_191, Gson 2.8.6.

1, rely on the introduction of

<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>

2, the sequence of

    public static String serialize() {
        MyBean bean = new MyBean();
        bean.setS("测试字符串");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        bean.setD(calendar.getTime());

        List<MyBean> list = new ArrayList<MyBean>();
        list.add(bean);
        Map<String, List<MyBean>> map = new HashMap<String, List<MyBean>>();
        map.put("data", list);

        gson gson= New new GsonBuilder () 
                .serializeNulls () // serialized field is null 
                .setDateFormat ( "the MM-dd-YYYY HH: mm: SS") // set the date format 
                .excludeFieldsWithModifiers (Modifier.STATIC) // The modified Fu filter field 
                .create (); 
        String Result = gson.toJson (Map); 
        System.out.println (Result); 
        return Result; 
    }

3, deserialization

    public  static  void Deserialize () { 
        String S = the serialize (); 
        Gson GSON = new new GsonBuilder () 
                .setDateFormat ( "the MM-dd-YYYY HH: mm: SS") // set the date format 
                .create (); 
        the Type type = new new TypeToken <the Map <String, List <>>> The MyBean () {} getType ();. 
        the Map <String, List <>> The MyBean Map =   gson.fromJson (S, type); 
        System.out.println (Map) ; 
        
        // Get data directly corresponding API 
        JsonElement Element = JsonParser.parseString (S); 
        JsonArray data= element.getAsJsonObject().getAsJsonArray("data");
        JsonObject bean = data.get(0).getAsJsonObject();
        System.out.println(bean.get("s").getAsString());
        //...
    }

4, complete example

package com.inspur.demo.json; 

import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map; 

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

/**
 * Gson操作JSON数据
 */
public class GsonCase {
    /**
     * 序列化
     */
    public static String serialize() {
        MyBean bean = new MyBean();
        bean.setS("测试字符串");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.HOUR_OF_DAY, -1);
        bean.setD(calendar.getTime());

        List<MyBean> list = new ArrayList<MyBean>();
        list.add(bean);
        Map<String, List<MyBean>> map = new new the HashMap <String, List <>> The MyBean (); 
        map.put ( "Data" , List); 

        Gson GSON = new new GsonBuilder () 
                .serializeNulls () // serialized field is null 
                .setDateFormat ( "yyyy- dd HH-the mM: mm: SS ") // set the date format 
                .excludeFieldsWithModifiers (Modifier.STATIC) // The modifier filter fields 
                .create (); 
        String Result = gson.toJson (Map); 
        System.out.println ( Result); 
        return Result; 
    } 

    / ** 
     * deserialize 
     * / 
    public  static void Deserialize () { 
        String S = the serialize (); 
        Gson GSON = new new GsonBuilder () 
                .setDateFormat ( "the MM-dd-YYYY HH: mm: SS") // set the date format 
                .create (); 
        the Type type = new new TypeToken <the Map <String, List <>>> The MyBean . () {} getType (); 
        the Map <String, List <>> The MyBean Map =   gson.fromJson (S, type); 
        System.out.println (Map); 
        
        / / direct access to the corresponding data through API 
        JsonElement Element = JsonParser.parseString (S); 
        JsonArray data= element.getAsJsonObject().getAsJsonArray("data");
        JsonObject bean = data.get(0).getAsJsonObject();
        System.out.println(bean.get("s").getAsString());
        //...
    }
    
    public static void main(String[] args) {
        serialize();
        deserialize();
    }

}
GsonCase
package com.inspur.demo.json;

import java.util.Arrays;
import java.util.Date;

public class MyBean {
    private String s = "字符串";
    private String s2;
    private String s3 = "字符串3";
    private int i = 123;
    private Integer i2;
    private char c = '到';
    private char[] c2 = new char[]{'a', 'b'};
    private Date d = new Date();
    private Date[] d2 = new Date[]{new Date(), new Date()};
    public String getS() {
        return s;
    }
    public void setS(String s) {
        this.s = s;
    }
    public String getS2() {
        return s2;
    }
    public void setS2(String s2) {
        this.s2 = s2;
    }
    public String getS3() {
        return s3;
    }
    public void setS3(String s3) {
        this.s3 = s3;
    }
    public int getI() {
        return i;
    }
    public void setI(int i) {
        this.i = i;
    }
    public Integer getI2() {
        return i2;
    }
    public void setI2(Integer i2) {
        this.i2 = i2;
    }
    public char getC() {
        return c;
    }
    public void setC(char c) {
        this.c = c;
    }
    public char[] getC2() {
        return c2;
    }
    public void setC2(char[] c2) {
        this.c2 = c2;
    }
    public Date getD() {
        return d;
    }
    public void setD(Date d) {
        this.d = d;
    }
    public Date[] getD2() {
        return d2;
    }
    public void setD2(Date[] d2) {
        this.d2 = d2;
    }
    @Override
    public String toString() {
        return "MyBean [s=" + s + ", s2=" + s2 + ", s3=" + s3 + ", i=" + i + ", i2=" + i2 + ", c=" + c + ", c2="
                + Arrays.toString(c2) + ", d=" + d + ", d2=" + Arrays.toString(d2) + "]";
    }
}
MyBean

 

Guess you like

Origin www.cnblogs.com/wuyongyin/p/12001016.html