| 注册
请输入搜索内容

热门搜索

Java Linux MySQL PHP JavaScript Hibernate jQuery Nginx
jopen
9年前发布

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只是不会自动提示,自己敲出来的是不会报错的,嗯,我就遇到这一个小小的问题,总体来说这个东东还是很好用的。

 本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
 转载本站原创文章,请注明出处,并保留原始链接、图片水印。
 本站是一个以用户分享为主的开源技术平台,欢迎各类分享!
 本文地址:https://www.open-open.com/lib/view/open1446431888326.html
JSON开发包 Gson