• gson使用详解


    昨天读一篇文章,看到gson这个词,一开始还以为作者写错了,问了度娘之后才发现是我才疏学浅,于是大概了解了一下gson用法,总体来说还是很简单的。

    Gson.jar下载

    JavaBean转json

        //javaBean-->json
        @Test
        public void test1(){
            Gson gson = new Gson();
            Person p = new Person("张三", "123", 500, new Book("三国演义", "罗贯中"));
            String json = gson.toJson(p);
            System.out.println(json);
        }

    json转javabean

        //json-->javaBean
        @Test
        public void test2(){
            String json = "{"username":"张三","password":"123","money":500,"book":{"id":0,"name":"三国演义","price":0,"author":"罗贯中"}}";
            Gson gson = new Gson();
            Person p = gson.fromJson(json, Person.class);
            System.out.println(p.getUsername()+"--"+p.getBook().getName());
        }

    List集合转json

        //list-->json
        @Test
        public void test3() {
            List<Person> list = new ArrayList<Person>();
            Person p = new Person("张三", "123", 500, new Book("三国演义", "罗贯中"));
            list.add(p);
            p = new Person("李四", "123", 500, new Book("三国演义", "罗贯中"));
            list.add(p);
            Gson gson = new Gson();
            String gsonStr = gson.toJson(list);
            System.out.println(gsonStr);
        }

    Map集合转json

        //map-->json
        @Test
        public void test4() {
            Map<String, Person> map = new HashMap<String, Person>();
            Person p = new Person("张三", "123", 500, new Book("三国演义", "罗贯中"));
            map.put("p1", p);
            p = new Person("李四", "123", 500, new Book("三国演义", "罗贯中"));
            map.put("p2", p);
            Gson gson = new Gson();
            String gsonStr = gson.toJson(map);
            System.out.println(gsonStr);
        }

    json字符串转List集合

        //json-->list
        @Test
        public void test5() {
            String str = "[{"username":"张三","password":"123","money":500,"book":{"id":0,"name":"三国演义","price":0,"author":"罗贯中"}},{"username":"李四","password":"123","money":500,"book":{"id":0,"name":"三国演义","price":0,"author":"罗贯中"}}]";
            Gson gson = new Gson();
            List<Person> ps = gson.fromJson(str,new TypeToken<List<Person>>() {  
            }.getType());
            for (Person person : ps) {
                System.out.println(person.getUsername()+"---"+person.getBook().getName());
            }
    
        }

    json字符串转Map集合

        //json-->map
        @Test
        public void test6(){
            String str = "{"p1":{"username":"张三","password":"123","money":500,"book":{"id":0,"name":"三国演义","price":0,"author":"罗贯中"}},"p2":{"username":"李四","password":"123","money":500,"book":{"id":0,"name":"红楼梦","price":0,"author":"曹雪芹"}}}";
            Gson gson = new Gson();
            Map<String, Person> map = gson.fromJson(str, new TypeToken<Map<String, Person>>(){}.getType());
            Set<String> keys = map.keySet();
            for(String key:keys){
                System.out.println(map.get(key).getUsername()+"--"+map.get(key).getBook().getName());
            }
        }

    相关的两个javabean
    Person.java

    public class Person {
    
        private String username;
        private String password;
        private int money;
        private Book book;
    
        public Person() {
        }
    
        public Person(Book book) {
            this.book = book;
        }
    
        public Book getBook() {
            return book;
        }
        public void setBook(Book book) {
            this.book = book;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public int getMoney() {
            return money;
        }
        public void setMoney(int money) {
            this.money = money;
        }
    
        public Person(String username, String password, int money, Book book) {
            super();
            this.username = username;
            this.password = password;
            this.money = money;
            this.book = book;
        }
    
    }

    Book.java

    public class Book {
    
        private int id;
        private String name;
        private int price;
        private String author;
        private Detail detail;
        private Attribute attribute;
    
        public Attribute getAttribute() {
            return attribute;
        }
        public void setAttribute(Attribute attribute) {
            this.attribute = attribute;
        }
        public Detail getDetail() {
            return detail;
        }
        public void setDetail(Detail detail) {
            this.detail = detail;
        }
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getPrice() {
            return price;
        }
        public void setPrice(int price) {
            this.price = price;
        }
        public String getAuthor() {
            return author;
        }
        public void setAuthor(String author) {
            this.author = author;
        }
        public Book(String name, String author) {
            this.name = name;
            this.author = author;
        }
        public Book() {
        }
    
    }

    注意事项:
    把json字符串转为集合时,new TypeToken<>(){}.getType()在eclipse中貌似不能够自动提示,如果你的电脑也是这样的话,建议这里的代码自己手动敲一下,eclipse只是不会自动提示,自己敲出来的是不会报错的,嗯,我就遇到这一个小小的问题,总体来说这个东东还是很好用的。

  • 相关阅读:
    百度编辑器图片在线流量返回url改动
    谋哥:悟马道长第一句话之“不要赚屌丝的钱”
    HDOJ 题目3518 Boring counting(后缀数组,求不重叠反复次数最少为2的子串种类数)
    mysqlbinlog高速遍历搜索记录
    推断整数、推断邮箱、推断手机号码、推断邮政编码、推断中文
    用筛选法求100以内的素数(数组)
    【DataStructure】One of queue usage: Simulation System
    Registry Connect failed,Windows服务诊断
    Struts2 的工作原理
    vue上传图片到服务器
  • 原文地址:https://www.cnblogs.com/qitian1/p/6461835.html
Copyright © 2020-2023  润新知