Gson 用于Java POJO与字符串序列化和反序列化

落花浮王杯 提交于 2020-07-25 06:26:21

1)pom.xml

  <dependencies>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>
    </dependencies>

2)ProtoMgr.java

package gson;

import com.google.gson.Gson;

public class ProtoMgr {
    private static final Gson gson = new Gson();

    public static String toJson(Object obj) {
        return gson.toJson(obj);
    }

    public static <T> T fromJson(String json, Class<T> clazz) {
        return gson.fromJson(json, clazz);
    }
}

3)POJO

RankItem.java

package gson;

public class RankItem {
    public int index;

    public RankItem() {
        this.index = (int) Math.floor(Math.random() * 1000);
    }
}

Rank.java

package gson;

import java.util.ArrayList;
import java.util.HashMap;

public class Rank {
    public int r;

    public ArrayList<RankItem> rankItems = new ArrayList<>();

    public HashMap<Integer, RankItem> hashMap = new HashMap<>();

    public Rank(int r) {
        this.r = r;

        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());
        rankItems.add(new RankItem());

        hashMap.put(1, new RankItem());
        hashMap.put(2, new RankItem());
        hashMap.put(3, new RankItem());
        hashMap.put(4, new RankItem());
    }
}

PlayerInfo.java

package gson;

import java.util.ArrayList;

public class PlayerInfo {
    public ArrayList<Rank> arrayList = new ArrayList<>();

    public PlayerInfo() {
        arrayList.add(new Rank(1));
        arrayList.add(new Rank(2));
        arrayList.add(new Rank(3));
    }
}

4)测试

package gson;

public class Main {
    public static void main(String[] args) {
        PlayerInfo playerInfo = new PlayerInfo();

        String jsonStr = ProtoMgr.toJson(playerInfo);
        System.out.println(jsonStr);

        PlayerInfo info = ProtoMgr.fromJson(jsonStr, PlayerInfo.class);
        System.out.println(info);

    }
}


5)输入

序列化为的字符串

{
    "arrayList":[
        {
            "r":1,
            "rankItems":[
                {
                    "index":819
                },
                {
                    "index":978
                },
                {
                    "index":675
                },
                {
                    "index":860
                },
                {
                    "index":655
                }
            ],
            "hashMap":{
                "1":{
                    "index":203
                },
                "2":{
                    "index":820
                },
                "3":{
                    "index":300
                },
                "4":{
                    "index":404
                }
            }
        },
        {
            "r":2,
            "rankItems":[
                {
                    "index":934
                },
                {
                    "index":424
                },
                {
                    "index":683
                },
                {
                    "index":186
                },
                {
                    "index":20
                }
            ],
            "hashMap":{
                "1":{
                    "index":548
                },
                "2":{
                    "index":794
                },
                "3":{
                    "index":562
                },
                "4":{
                    "index":152
                }
            }
        },
        {
            "r":3,
            "rankItems":[
                {
                    "index":478
                },
                {
                    "index":655
                },
                {
                    "index":704
                },
                {
                    "index":564
                },
                {
                    "index":477
                }
            ],
            "hashMap":{
                "1":{
                    "index":243
                },
                "2":{
                    "index":843
                },
                "3":{
                    "index":711
                },
                "4":{
                    "index":461
                }
            }
        }
    ]
}

json转为POJO

 

 

 

 

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!