手边星辰

博客园 首页 新随笔 联系 订阅 管理

demo类

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class DateInfo {

    
    private String mockDateTime;

    private String serverDateTime;

    private String currentDate;


}

 测试类

import com.citi.cv.cisportal.authsite.controller.managestrategy.module.DateInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        Map<String, DateInfo> map = new HashMap<>();
        map.put("Key1", DateInfo.builder().currentDate(System.currentTimeMillis() + "_1").build());
        map.put("Key2", DateInfo.builder().currentDate(System.currentTimeMillis() + "_2").build());
        map.put("Key3", DateInfo.builder().currentDate(System.currentTimeMillis() + "_3").build());

        Type type = new TypeToken<HashMap<String, DateInfo>>() {
        }.getType();

        Gson gson = new Gson();
        String json = gson.toJson(map, type);
        System.out.println("gson.toJson(map) = " + json);

        Map<String, DateInfo> fromJson = gson.fromJson(json, type);
        System.out.println("fromJson = " + fromJson);

        System.out.println("fromJson.equals(map) = " + fromJson.equals(map));
        System.out.println("fromJson.get(\"Key1\").equals(map.get(\"Key1\")) = " + fromJson.get("Key1").equals(map.get("Key1")));

    }

}

 

 

输出结果 

gson.toJson(map) = {"Key2":{"currentDate":"1575444488009_2"},"Key1":{"currentDate":"1575444488009_1"},"Key3":{"currentDate":"1575444488009_3"}}
fromJson = {Key2=DateInfo(mockDateTime=null, serverDateTime=null, currentDate=1575444488009_2), Key1=DateInfo(mockDateTime=null, serverDateTime=null, currentDate=1575444488009_1), Key3=DateInfo(mockDateTime=null, serverDateTime=null, currentDate=1575444488009_3)}
fromJson.equals(map) = true
fromJson.get("Key1").equals(map.get("Key1")) = true

 

posted on 2019-12-03 17:04  手边星辰  阅读(960)  评论(0编辑  收藏  举报