gson使用详解
【摘要】 昨天读一篇文章,看到gson这个词,一开始还以为作者写错了,问了度娘之后才发现是我才疏学浅,于是大概了解了一下gson用法,总体来说还是很简单的。
Gson.jar下载
JavaBean转json
//javaBean-->json @Test public void test1(){ Gson gson = new Gson(); Person p = ...
昨天读一篇文章,看到gson这个词,一开始还以为作者写错了,问了度娘之后才发现是我才疏学浅,于是大概了解了一下gson用法,总体来说还是很简单的。
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); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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()); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
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); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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()); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
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()); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
相关的两个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; }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
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() { }
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
注意事项:
把json字符串转为集合时,new TypeToken<>(){}.getType()
在eclipse中貌似不能够自动提示,如果你的电脑也是这样的话,建议这里的代码自己手动敲一下,eclipse只是不会自动提示,自己敲出来的是不会报错的,嗯,我就遇到这一个小小的问题,总体来说这个东东还是很好用的。
文章来源: wangsong.blog.csdn.net,作者:_江南一点雨,版权归原作者所有,如需转载,请联系作者。
原文链接:wangsong.blog.csdn.net/article/details/46043549
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)