Insurance 项目——与移动端交互json-Gson

Posted kaka_79

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Insurance 项目——与移动端交互json-Gson相关的知识,希望对你有一定的参考价值。

这是一个java初学者在独立开发一个项目时做的笔记,很多内容仅仅是为了解决当前需求,并未很深入的研究。


什么是Json。

Json是开发领域数据传递的一种。有着数据结构简单,数据字节长度短等优势。

http://www.json.org.cn/

 

这里主要是用来和移动端做交互,使用了google的Gson。

1、pom

技术分享
1  <!-- Gson -->
2     <dependency>
3         <groupId>com.google.code.gson</groupId>
4         <artifactId>gson</artifactId>
5         <version>2.3</version>
6     </dependency>
pom.xml

2、序列化

   序列化是将对象状态转换为可保持或传输的格式的过程。在这里就是将对象转换成为json。

实体类:

 

技术分享
 1 public class User {
 2     private Integer id;
 3 
 4     private String userName;
 5 
 6     private String password;
 7 
 8     private Integer age;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getUserName() {
19         return userName;
20     }
21 
22     public void setUserName(String userName) {
23         this.userName = userName == null ? null : userName.trim();
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password == null ? null : password.trim();
32     }
33 
34     public Integer getAge() {
35         return age;
36     }
37 
38     public void setAge(Integer age) {
39         this.age = age;
40     }
41 }
User

 

测试类:

技术分享
 1    @Test
 2     public void test(){
 3         Gson g = new Gson();
 4         User u = new User();
 5         u.setAge(10);
 6         u.setId(5);
 7         u.setPassword("password");
 8         u.setUserName("username");
 9         String ugson = g.toJson(u);
10         System.out.println(ugson);
11     }
Test

打印结果:{"id":5,"userName":"username","password":"password","age":10}

3、反序列化

  反序列化就是将json转换成为对象。

沿用User实体类。

Json:{"id":10,"userName":"jack","password":"jack","age":15}

测试类:

技术分享
1  @Test
2     public void test(){
3         String Ujson = "{\"id\":10,\"userName\":\"jack\",\"password\":\"jack\",\"age\":15}";
4         Gson g = new Gson();
5         User u = g.fromJson(Ujson, User.class);
6         System.out.println("id:"+u.getId()+",age:"+u.getAge()+",username:"+u.getUserName()+",password:"+u.getPassword());
7     }
Test

打印结果:id:10,age:15,username:jack,password:jack

4、@Expose

  在实体类中,可以给属性加入@Expose注解。在使用GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create();序列化时,不会对未注解的属性进行序列化。

实体类:

技术分享
 1 public class User {
 2     private Integer id;
 3     @Expose
 4     private String userName;
 5 
 6     private String password;
 7     @Expose
 8     private Integer age;
 9 
10     public Integer getId() {
11         return id;
12     }
13 
14     public void setId(Integer id) {
15         this.id = id;
16     }
17 
18     public String getUserName() {
19         return userName;
20     }
21 
22     public void setUserName(String userName) {
23         this.userName = userName == null ? null : userName.trim();
24     }
25 
26     public String getPassword() {
27         return password;
28     }
29 
30     public void setPassword(String password) {
31         this.password = password == null ? null : password.trim();
32     }
33 
34     public Integer getAge() {
35         return age;
36     }
37 
38     public void setAge(Integer age) {
39         this.age = age;
40     }
41 }
User

测试类:

技术分享
 1  @Test
 2     public void test(){
 3         Gson g = new Gson();
 4         Gson g1 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 5         User u = new User();
 6         u.setAge(10);
 7         u.setId(5);
 8         u.setPassword("password");
 9         u.setUserName("username");
10         String ugson = g.toJson(u);
11         String u1gson = g1.toJson(u);
12         System.out.println("g:"+ugson);
13         System.out.println("g1:"+u1gson);
14     }
Test

打印结果:

g:{"id":5,"userName":"username","password":"password","age":10}
g1:{"userName":"username","age":10}

 

5、@SerializedName

  用来定义属性序列化后的名字。

实体类:

技术分享
 1 public class User {
 2     private Integer id;
 3     @Expose
 4     @SerializedName("name")
 5     private String userName;
 6     @Expose
 7     @SerializedName("pass_word")
 8     private String password;
 9     @Expose
10     private Integer age;
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getUserName() {
21         return userName;
22     }
23 
24     public void setUserName(String userName) {
25         this.userName = userName == null ? null : userName.trim();
26     }
27 
28     public String getPassword() {
29         return password;
30     }
31 
32     public void setPassword(String password) {
33         this.password = password == null ? null : password.trim();
34     }
35 
36     public Integer getAge() {
37         return age;
38     }
39 
40     public void setAge(Integer age) {
41         this.age = age;
42     }
43 }
User

测试类:

技术分享
 1     @Test
 2     public void test(){
 3         Gson g = new Gson();
 4         Gson g1 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 5         User u = new User();
 6         u.setAge(10);
 7         u.setId(5);
 8         u.setPassword("password");
 9         u.setUserName("username");
10         String ugson = g.toJson(u);
11         String u1gson = g1.toJson(u);
12         System.out.println("g:"+ugson);
13         System.out.println("g1:"+u1gson);
14     }
Test

打印结果:

g:{"id":5,"name":"username","pass_word":"password","age":10}
g1:{"name":"username","pass_word":"password","age":10}

6、@Expose,@SerializedName 反序列化

实体类:

技术分享
 1 public class User {
 2     private Integer id;
 3     @Expose
 4     @SerializedName("name")
 5     private String userName;
 6     @Expose
 7     @SerializedName("pass_word")
 8     private String password;
 9     @Expose
10     private Integer age;
11 
12     public Integer getId() {
13         return id;
14     }
15 
16     public void setId(Integer id) {
17         this.id = id;
18     }
19 
20     public String getUserName() {
21         return userName;
22     }
23 
24     public void setUserName(String userName) {
25         this.userName = userName == null ? null : userName.trim();
26     }
27 
28     public String getPassword() {
29         return password;
30     }
31 
32     public void setPassword(String password) {
33         this.password = password == null ? null : password.trim();
34     }
35 
36     public Integer getAge() {
37         return age;
38     }
39 
40     public void setAge(Integer age) {
41         this.age = age;
42     }
43 }
User

测试类:

技术分享
 1  @Test
 2     public void test(){
 3         Gson g = new Gson();
 4         Gson g1 = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
 5         String Ujson = "{\"id\":10,\"userName\":\"jack\",\"password\":\"jack\",\"age\":15}";
 6         String U1json = "{\"id\":10,\"name\":\"jack\",\"pass_word\":\"jack\",\"age\":15}";
 7         User gUser = g.fromJson(Ujson, User.class);
 8         User g1User = g1.fromJson(Ujson, User.class);
 9         User gU1ser = g.fromJson(U1json, User.class);
10         User g1U1ser = g1.fromJson(U1json, User.class);
11         System.out.println("gUser:{id:"+gUser.getId()+",age:"+gUser.getAge()+",username:"+gUser.getUserName()+",password:"+gUser.getPassword()+"}");
12         System.out.println("g1User:{id:"+g1User.getId()+",age:"+g1User.getAge()+",username:"+g1User.getUserName()+",password:"+g1User.getPassword()+"}");
13         System.out.println("gU1ser:{id:"+gU1ser.getId()+",age:"+gU1ser.getAge()+",username:"+gU1ser.getUserName()+",password:"+gU1ser.getPassword()+"}");
14         System.out.println("g1U1ser:{id:"+g1U1ser.getId()+",age:"+g1U1ser.getAge()+",username:"+g1U1ser.getUserName()+",password:"+g1U1ser.getPassword()+"}");
15     }
Test

打印结果:

gUser:{id:10,age:15,username:null,password:null}
g1User:{id:null,age:15,username:null,password:null}
gU1ser:{id:10,age:15,username:jack,password:jack}
g1U1ser:{id:null,age:15,username:jack,password:jack}

 

使用了@SerializedName 注解来定义属性名,必须使用。

@Expose可以选择使用New Gson()不用;

 







以上是关于Insurance 项目——与移动端交互json-Gson的主要内容,如果未能解决你的问题,请参考以下文章

移动端基于HTML模板和JSON数据的JavaScript交互

移动端h5截图与原生iosandroid的兼容交互

移动端h5截图与原生iosandroid的兼容交互

移动端h5截图与原生iosandroid的兼容交互

移动端h5截图与原生iosandroid的兼容交互

Java之基于S2SH与手机数据交互