Java基础98 gson插件的使用

1、要用到的包  

概念:JSON是javascript对象的一种形态,是一种轻量级的数据交换格式。一般用来在Java EE后台服务和Android或者ios、HTML5之间进行通信的一种数据传递的格式。

     Gson是谷歌推出的一个用于生成和解析JSON数据格式的工具;

2、实例        

实体类 people

 1 package com.shore.entity;
 2 
 3 /**
 4  * @author DSHORE/2019-4-21
 5  *
 6  */
 7 public class People {
 8     private int pid;
 9     private String pname;
10     private int age;
11     private String job;
12     private double sal;
13     
14     public People() {
15         
16     }
17     public People(int pid, String pname, int age, String job, double sal) {
18         super();
19         this.pid = pid;
20         this.pname = pname;
21         this.age = age;
22         this.job = job;
23         this.sal = sal;
24     }
25     public int getPid() {
26         return pid;
27     }
28     public void setPid(int pid) {
29         this.pid = pid;
30     }
31     public String getPname() {
32         return pname;
33     }
34     public void setPname(String pname) {
35         this.pname = pname;
36     }
37     public int getAge() {
38         return age;
39     }
40     public void setAge(int age) {
41         this.age = age;
42     }
43     public String getJob() {
44         return job;
45     }
46     public void setJob(String job) {
47         this.job = job;
48     }
49     public double getSal() {
50         return sal;
51     }
52     public void setSal(double sal) {
53         this.sal = sal;
54     }
55     @Override
56     public String toString() {
57         return "People [pid=" + pid + ", pname=" + pname + ", age=" + age
58                 + ", job=" + job + ", sal=" + sal + "]";
59     }
60 }

gson 测试类

 1 package com.shore.test;
 2 
 3 import java.lang.reflect.Type;
 4 import java.util.ArrayList;
 5 import java.util.List;
 6 
 7 import net.sf.json.JSONObject;
 8 
 9 import com.google.gson.Gson;
10 import com.google.gson.reflect.TypeToken;
11 import com.shore.entity.People;
12 
13 /**
14  * @author DSHORE/2019-4-21
15  *
16  */
17 public class GsonTest {
18     public static void main(String[] args) {
19         People p = new People();
20         p.setAge(18);
21         p.setPname("张三");
22         p.setJob("工程师");
23         p.setPid(1);
24         p.setSal(10000);
25         
26         People p2 = new People();
27         p2.setAge(20);
28         p2.setPname("李四");
29         p2.setJob("设计师");
30         p2.setPid(2);
31         p2.setSal(8000);
32         
33         People p3 = new People();
34         p3.setAge(20);
35         p3.setPname("王五");
36         p3.setJob("工程师");
37         p3.setPid(3);
38         p3.setSal(15000);
39         
40         //1.将java对象转为json字符串
41         Gson gson = new Gson();
42         String json = gson.toJson(p);
43         System.out.println(json); //返回值:{"pid":1,"pname":"张三","age":18,"job":"工程师","sal":10000.0}
44         
45         //2.将json字符串转为java对象
46         json = "{\"age\":18,\"job\":\"工程师\",\"pid\":1,\"pname\":\"张三\",\"sal\":10000}";
47         People people = gson.fromJson(json, People.class);
48         System.out.println(people);//返回值:People [pid=1, pname=张三, age=18, job=工程师, sal=10000.0]
49         //p2对象
50         JSONObject jsonObject = JSONObject.fromObject(p2);
51         System.out.println(jsonObject);//返回值:{"age":20,"job":"设计师","pid":2,"pname":"李四","sal":8000}
52         
53         //3.将一个对象集合转为json
54         List<People> list = new ArrayList<People>();
55         list.add(p);
56         list.add(p2);
57         list.add(p3);
58         json=gson.toJson(list);
59         System.out.println(json);//返回值:[{"pid":1,"pname":"张三","age":18,"job":"工程师","sal":10000.0},{"pid":2,"pname":"李四","age":20,"job":"设计师","sal":8000.0},{"pid":3,"pname":"王五","age":20,"job":"工程师","sal":15000.0}]
60         
61         //4.将json字符串转为带泛型集合对象
62         Type listType=new TypeToken<List<People>>(){}.getType();
63         List<People> list2=gson.fromJson(json,listType);
64         System.out.println(list2.get(2).getPname());//返回值:王五          get(2)中的2表示:下标值
65     }
66 }

 

json插件的使用(java对象和json字符串对象之间的转换)

 

 

 

 

 

原创作者:DSHORE

作者主页:http://www.cnblogs.com/dshore123/

原文出自:https://www.cnblogs.com/dshore123/p/10746722.html

欢迎转载,转载务必说明出处。(如果本文对您有帮助,可以点击一下右下角的 推荐,或评论,谢谢!

posted @ 2019-04-21 20:26  DSHORE  阅读(482)  评论(0编辑  收藏  举报