GSON入门篇(内含教学视频+源代码)
教学视频+源代码下载链接地址:
https://download.csdn.net/download/weixin_46411355/87474475
目录
- GSON入门篇(内含教学视频+源代码)
- `教学视频+源代码下载链接地址:`[https://download.csdn.net/download/weixin_46411355/87474475](https://download.csdn.net/download/weixin_46411355/87474475)
- 一、创建一个JavaSE项目
- 二、IDEA中导入gson的jar包
- 三、创建JavaBean
- 四、测试类
- 4.1 一个对象转JSON
- 4.2 多个对象转JSON
- 4.3 JSON字符串转一个对象
- 4.4 JSON字符串转多个对象(List)
- 4.4.1 错误案例
- 4.4.2 正确案例
一、创建一个JavaSE项目
二、IDEA中导入gson的jar包
笔者的gson jar包的下载连接地址:https://download.csdn.net/download/weixin_46411355/87474404
复习:IDEA导入第三方jar包的方式。可以查看笔者的另一篇博文《IDEA导入和删除第三方jar包》——https://huanghaoheng.blog.csdn.net/article/details/126332558
创建一个lib目录,并将下载好的jar包放进lib目录中
点击File->Project Structure
点击Modules
点击你的项目或Module
点击Dependencies
点击“+”
点击“1 JARs or directories”
选择你jar包所在的路径,然后点击ok
再点击ok
同时再导入lombok和Junit的jar包,步骤一样,此处省略
笔者的lombok jar包下载链接地址:https://download.csdn.net/download/weixin_46411355/87474401
笔者的Junit jar包下载链接地址:https://download.csdn.net/download/weixin_46411355/87398726
三、创建JavaBean
User.java
package com.bjpowernode.domain;
import com.oracle.webservices.internal.api.databinding.DatabindingMode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Data
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private Integer age;
}
四、测试类
4.1 一个对象转JSON
/**
* 测试一个对象转JSON
*/
@Test
public void testOneObjectConvertToJSON(){
User user = new User(1,"zhangsan","123456","三丰大师",149) ;
Gson gson = new Gson();
//写法一
System.out.println("=======================写法一=========================");
String userJson = gson.toJson(user, User.class);
System.out.println("userJson = " + userJson);//userJson = {"id":1,"username":"zhangsan","password":"123456","nickname":"三丰大师","age":149}
//写法二
System.out.println("========================写法二=========================");
String userJson2 = gson.toJson(user);
System.out.println("userJson2 = " + userJson2);
}
4.2 多个对象转JSON
/**
* 测试多个对象转JSON
*/
@Test
public void testManyObjectConvertToJSON(){
List<User> users = new ArrayList<>();
User user = null;
for (int i = 1; i <=5 ; i++) {
user = new User(i,"hhh"+i,123456+"i","哈哈哈"+i,24+i);
users.add(user);
}
Gson gson = new Gson();
String usersJson = gson.toJson(users);
System.out.println("usersJson = " + usersJson);
}
4.3 JSON字符串转一个对象
/**
* 测试JSON字符串转一个对象
*/
@Test
public void testJSONSConvertToOneObject(){
//如何将JSON字符串转换成对象
String jsonStr = "{\"id\":1,\"username\":\"zhangsan\",\"password\":\"123456\",\"nickname\":\"三丰大师\",\"age\":149}";
Gson gson = new Gson();
User user = gson.fromJson(jsonStr, User.class);
System.out.println("user = " + user);
}
4.4 JSON字符串转多个对象(List)
4.4.1 错误案例
/**
* 测试JSON字符串转多个对象(List)(错误案例)
*/
@Test
public void testJSONConvertManyObjectErrorInstance(){
String jsonStr = "[{\"id\":1,\"username\":\"hhh1\",\"password\":\"123456i\",\"nickname\":\"哈哈哈1\",\"age\":25},{\"id\":2,\"username\":\"hhh2\",\"password\":\"123456i\",\"nickname\":\"哈哈哈2\",\"age\":26},{\"id\":3,\"username\":\"hhh3\",\"password\":\"123456i\",\"nickname\":\"哈哈哈3\",\"age\":27},{\"id\":4,\"username\":\"hhh4\",\"password\":\"123456i\",\"nickname\":\"哈哈哈4\",\"age\":28},{\"id\":5,\"username\":\"hhh5\",\"password\":\"123456i\",\"nickname\":\"哈哈哈5\",\"age\":29}]\n";
Gson gson = new Gson();
ArrayList<User> arrayList = gson.fromJson(jsonStr, ArrayList.class);
/*上面这行代码,使得结果输出如下:
* {id=1.0, username=hhh1, password=123456i, nickname=哈哈哈1, age=25.0}
{id=2.0, username=hhh2, password=123456i, nickname=哈哈哈2, age=26.0}
{id=3.0, username=hhh3, password=123456i, nickname=哈哈哈3, age=27.0}
{id=4.0, username=hhh4, password=123456i, nickname=哈哈哈4, age=28.0}
{id=5.0, username=hhh5, password=123456i, nickname=哈哈哈5, age=29.0}
输出的结果,既不是对象,也不是JSON字符串
对象 :User(id=1, username=zhangsan, password=123456, nickname=三丰大师, age=149)
JSON字符串 : {"id":1,"username":"zhangsan","password":"123456","nickname":"三丰大师","age":149}
*/
// for (Object o : arrayList) {
// System.out.println(o);
// }
arrayList.forEach(System.out::println);
}
4.4.2 正确案例
@Test
public void testJSONConvertManyObjectCorrectInstance(){
String jsonStr = "[{\"id\":1,\"username\":\"hhh1\",\"password\":\"123456i\",\"nickname\":\"哈哈哈1\",\"age\":25},{\"id\":2,\"username\":\"hhh2\",\"password\":\"123456i\",\"nickname\":\"哈哈哈2\",\"age\":26},{\"id\":3,\"username\":\"hhh3\",\"password\":\"123456i\",\"nickname\":\"哈哈哈3\",\"age\":27},{\"id\":4,\"username\":\"hhh4\",\"password\":\"123456i\",\"nickname\":\"哈哈哈4\",\"age\":28},{\"id\":5,\"username\":\"hhh5\",\"password\":\"123456i\",\"nickname\":\"哈哈哈5\",\"age\":29}]\n";
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<User>>(){}.getType();
ArrayList<User> arrayList = gson.fromJson(jsonStr, type);
// for (User user : arrayList) {
// System.out.println(user);
// }
arrayList.forEach(System.out::println);
/* 输出结果如下
* User(id=1, username=hhh1, password=123456i, nickname=哈哈哈1, age=25)
User(id=2, username=hhh2, password=123456i, nickname=哈哈哈2, age=26)
User(id=3, username=hhh3, password=123456i, nickname=哈哈哈3, age=27)
User(id=4, username=hhh4, password=123456i, nickname=哈哈哈4, age=28)
User(id=5, username=hhh5, password=123456i, nickname=哈哈哈5, age=29)
*
*/
}