Java鎿嶄綔JSON鏁版嵁--Gson鎿嶄綔JSON鏁版嵁

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java鎿嶄綔JSON鏁版嵁--Gson鎿嶄綔JSON鏁版嵁相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/uil' title='uil'>uil   serial   private   div   put   nbsp   lan   dep   json   

Gson鏄疓oogle鍏徃鍙戝竷鐨勪竴涓紑鍙戞簮鐮佺殑Java搴擄紝鍙敤浜庡皢Java瀵硅薄杞崲涓篔SON瀛楃涓诧紝涔熷彲鐢ㄤ簬灏咼SON瀛楃涓茶浆鎹负瀵瑰簲鐨凧ava瀵硅薄銆傛湰浠嬬粛涓婫son鐨勫熀鏈娇鐢ㄦ柟娉曪紝鍖呮嫭搴忓垪鍖栧拰鍙嶅簭鍒楀寲锛涙枃涓墍浣跨敤鍒扮殑杞欢鐗堟湰锛欽ava 1.8.0_191銆丟son 2.8.6銆?/p>

1銆佸紩鍏ヤ緷璧?/h2>
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.8.6</version>
</dependency>

2銆佸簭鍒楀寲

    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 GsonBuilder()
                .serializeNulls() //搴忓垪鍖栧€间负null鐨勫瓧娈?/span>
                .setDateFormat("yyyy-MM-dd HH:mm:ss") //璁剧疆鏃ユ湡鏍煎紡
                .excludeFieldsWithModifiers(Modifier.STATIC)//鏍规嵁淇グ绗﹁繃婊ゅ瓧娈?/span>
                .create();
        String result = gson.toJson(map);
        System.out.println(result);
        return result;
    }

3銆佸弽搴忓垪鍖?/h2>
    public static void deserialize() {
        String s = serialize();
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss") //璁剧疆鏃ユ湡鏍煎紡
                .create();
        Type type = new TypeToken<Map<String, List<MyBean>>>(){}.getType();
        Map<String, List<MyBean>> map =  gson.fromJson(s, type);
        System.out.println(map);
        
        //鐩存帴閫氳繃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銆佸畬鏁翠緥瀛?/h2>
鎶€鏈浘鐗? id=
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 HashMap<String, List<MyBean>>();
        map.put("data", list);

        Gson gson = new GsonBuilder()
                .serializeNulls() //搴忓垪鍖栧€间负null鐨勫瓧娈?/span>
                .setDateFormat("yyyy-MM-dd HH:mm:ss") //璁剧疆鏃ユ湡鏍煎紡
                .excludeFieldsWithModifiers(Modifier.STATIC)//鏍规嵁淇グ绗﹁繃婊ゅ瓧娈?/span>
                .create();
        String result = gson.toJson(map);
        System.out.println(result);
        return result;
    }

    /**
     * 鍙嶅簭鍒楀寲
     */
    public static void deserialize() {
        String s = serialize();
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss") //璁剧疆鏃ユ湡鏍煎紡
                .create();
        Type type = new TypeToken<Map<String, List<MyBean>>>(){}.getType();
        Map<String, List<MyBean>> map =  gson.fromJson(s, type);
        System.out.println(map);
        
        //鐩存帴閫氳繃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
鎶€鏈浘鐗? id=
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 = "瀛楃涓?";
    private int i = 123;
    private Integer i2;
    private char c = 鈥樺埌鈥?span style="color: #000000;">;
    private char[] c2 = new char[]{鈥榓鈥? 鈥榖鈥?span style="color: #000000;">};
    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

 

以上是关于Java鎿嶄綔JSON鏁版嵁--Gson鎿嶄綔JSON鏁版嵁的主要内容,如果未能解决你的问题,请参考以下文章

MySQL鐨凷QL璇彞 - 鏁版嵁鎿嶄綔璇彞锛?锛? DO 璇彞

JDBC閫氱敤鐨勬暟鎹簱鏁版嵁鎿嶄綔[CRUD]灏佽

MySQL-鍩烘湰鎿嶄綔

JMeter 涔嬫暟鎹簱鎿嶄綔鍙婂弬鏁板寲

Hive 鎿嶄綔涓庡簲鐢?璇嶉缁熻

妯℃澘璁捐鏃剁編宸ョ殑甯哥敤鎿嶄綔鏂瑰紡