Java基础98 gson插件的使用

时间:2023-12-09 21:38:25

1、要用到的包

Java基础98 gson插件的使用

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

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

2、实例

实体类 people

 package com.shore.entity;

 /**
* @author DSHORE/2019-4-21
*
*/
public class People {
private int pid;
private String pname;
private int age;
private String job;
private double sal; public People() { }
public People(int pid, String pname, int age, String job, double sal) {
super();
this.pid = pid;
this.pname = pname;
this.age = age;
this.job = job;
this.sal = sal;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
@Override
public String toString() {
return "People [pid=" + pid + ", pname=" + pname + ", age=" + age
+ ", job=" + job + ", sal=" + sal + "]";
}
}

gson 测试类

 package com.shore.test;

 import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List; import net.sf.json.JSONObject; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.shore.entity.People; /**
* @author DSHORE/2019-4-21
*
*/
public class GsonTest {
public static void main(String[] args) {
People p = new People();
p.setAge(18);
p.setPname("张三");
p.setJob("工程师");
p.setPid(1);
p.setSal(10000); People p2 = new People();
p2.setAge(20);
p2.setPname("李四");
p2.setJob("设计师");
p2.setPid(2);
p2.setSal(8000); People p3 = new People();
p3.setAge(20);
p3.setPname("王五");
p3.setJob("工程师");
p3.setPid(3);
p3.setSal(15000); //1.将java对象转为json字符串
Gson gson = new Gson();
String json = gson.toJson(p);
System.out.println(json); //返回值:{"pid":1,"pname":"张三","age":18,"job":"工程师","sal":10000.0} //2.将json字符串转为java对象
json = "{\"age\":18,\"job\":\"工程师\",\"pid\":1,\"pname\":\"张三\",\"sal\":10000}";
People people = gson.fromJson(json, People.class);
System.out.println(people);//返回值:People [pid=1, pname=张三, age=18, job=工程师, sal=10000.0]
//p2对象
JSONObject jsonObject = JSONObject.fromObject(p2);
System.out.println(jsonObject);//返回值:{"age":20,"job":"设计师","pid":2,"pname":"李四","sal":8000} //3.将一个对象集合转为json
List<People> list = new ArrayList<People>();
list.add(p);
list.add(p2);
list.add(p3);
json=gson.toJson(list);
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}] //4.将json字符串转为带泛型集合对象
Type listType=new TypeToken<List<People>>(){}.getType();
List<People> list2=gson.fromJson(json,listType);
System.out.println(list2.get(2).getPname());//返回值:王五 get(2)中的2表示:下标值
}
}

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

原创作者:DSHORE

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

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

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