实体和json

List list = new ArrayList<>();
        list.add(new Student("小张","男",20,"读书"));
        list.add(new Student("小明","男",20,"跑步"));
        list.add(new Student("小红","女",20,"旅游"));
        list.add(new Student("小白","男",20,"唱歌"));

        //将list集合序列化
        String s = new Gson().toJson(list);
        System.out.println("序列化为:"+s);

        //将list集合反序列化
        Type type =new TypeToken>(){
        }.getType();
        List list1 = new Gson().fromJson(s,type);
        System.out.println(list1.get(1).getName());
 

你可能感兴趣的:(windows,服务器,linux)