简介Gson使用(一)

本文详细介绍了Gson库如何简化Java对象与JSON字符串之间的转换过程,并通过示例展示了如何处理简单的类、数组对象、复杂的类结构、List集合、Map集合以及包含Map作为成员的类。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gson是google提供的开放源代码的java类库,主要用途为java对象与json字符串的串行化或者反串行化(通俗点就是,就是对象和json的相互转换)。

相对于使用json-lib.jar包来讲,Gson使用起来轻松许多了。


提供一个在线json校验网址,这样对于json字符串你很容易直观的了解其数据结构。

json校验网站



简单的类,成员不复杂。

       首先定义一个简单的鸟类。

/**
 * 定义了鸟类
 */
public class Bird {
	/** 名字 */
	private String BName;
	/** 年龄 */
	private int BAge;

	public Bird() {
		super();
	}

	public Bird(String bName, int bAge) {
		super();
		BName = bName;
		BAge = bAge;
	}

	public String getBName() {
		return BName;
	}

	public void setBName(String bName) {
		BName = bName;
	}

	public int getBAge() {
		return BAge;
	}

	public void setBAge(int bAge) {
		BAge = bAge;
	}

	@Override
	public String toString() {
		return "Bird [BName=" + BName + ", BAge=" + BAge + "]";
	}

}
          然后,使用Gson进行转换。

// 获取gson实例
		Gson gson = new Gson();
		Bird bird = new Bird("An", 1);

		// 将对象转换为json字符串
		String bgson = gson.toJson(bird);
		System.out.println(bgson);

		// 将json字符串转换为对象
		Bird sbird = gson.fromJson(bgson, Bird.class);
		System.out.println(sbird.toString());

       打印的结果:



数组对象:

       转换的代码:

// 获取gson实例
		Gson gson = new Gson();
		Bird abird = new Bird("An", 1);
		Bird bbird = new Bird("Bb", 2);

		// 将对象转换为json字符串
		String bgson = gson.toJson(new Bird[] { abird, bbird });
		System.out.println(bgson);

		// 将json字符串转换为对象
		Bird[] arrbird = gson.fromJson(bgson, Bird[].class);
		for (Bird b : arrbird) {
			System.out.println(b.toString());
		}

     打印结果:


复杂的对象,有类作为类的成员变量的情况。

       定义类:

/**
 * 定义了鸟类
 */
public class Bird {
	/** 名字 */
	private String BName;
	/** 年龄 */
	private int BAge;
	/** 鸟蛋的属性 */
	private BirdEgg BEgg;

	public Bird() {
		super();
	}

	public Bird(String bName, int bAge) {
		super();
		BName = bName;
		BAge = bAge;
	}

	public Bird(String bName, int bAge, BirdEgg bEgg) {
		super();
		BName = bName;
		BAge = bAge;
		BEgg = bEgg;
	}

	public String getBName() {
		return BName;
	}

	public void setBName(String bName) {
		BName = bName;
	}

	public int getBAge() {
		return BAge;
	}

	public void setBAge(int bAge) {
		BAge = bAge;
	}

	public BirdEgg getBEgg() {
		return BEgg;
	}

	public void setBEgg(BirdEgg bEgg) {
		BEgg = bEgg;
	}

	@Override
	public String toString() {
		return "Bird [BName=" + BName + ", BAge=" + BAge + ", BEgg=" + BEgg
				+ "]";
	}

}

/**
 * 鸟蛋类
 */
class BirdEgg {
	/** 蛋壳的颜色 */
	private String EColor;
	/** 蛋的重量 */
	private int EWeight;

	public BirdEgg() {
		super();
	}

	public BirdEgg(String eColor, int eWeight) {
		super();
		EColor = eColor;
		EWeight = eWeight;
	}

	public String getEColor() {
		return EColor;
	}

	public void setEColor(String eColor) {
		EColor = eColor;
	}

	public int getEWeight() {
		return EWeight;
	}

	public void setEWeight(int eWeight) {
		EWeight = eWeight;
	}

	@Override
	public String toString() {
		return "BirdEgg [EColor=" + EColor + ", EWeight=" + EWeight + "]";
	}
}

         解析:

// 获取gson实例
		Gson gson = new Gson();
		BirdEgg bEgg = new BirdEgg("white", 50);
		Bird bird = new Bird("An", 1, bEgg);

		// 将对象转换为json字符串
		String bgson = gson.toJson(bird);
		System.out.println(bgson);

		// 将json字符串转换为对象
		Bird abird = gson.fromJson(bgson, Bird.class);
		System.out.println(abird.toString());

       打印结果:



List集合:

// 获取gson实例
		Gson gson = new Gson();

		List<Bird> bList = new ArrayList<Bird>();
		Bird abird = new Bird("An", 1);
		Bird bbird = new Bird("Bn", 2);
		bList.add(abird);
		bList.add(bbird);

		// 将对象转换为json字符串
		String bgson = gson.toJson(bList);
		System.out.println(bgson);

		// 将json字符串转换为对象
		List<Bird> cList = new ArrayList<Bird>();
		Type listType = new TypeToken<List<Bird>>() {
		}.getType();
		cList = gson.fromJson(bgson, listType);
		for (Bird b : cList) {
			System.out.println(b.toString());
		}

       打印结果:


Map集合:

// 获取gson实例
		Gson gson = new Gson();
		Map<String, Bird> birdMap = new HashMap<String, Bird>();
		Bird abird = new Bird("An", 1);
		Bird bbird = new Bird("Bb", 2);
		birdMap.put("abird", abird);
		birdMap.put("bbird", bbird);

		// 将对象转换为json字符串
		String bgson = gson.toJson(birdMap);
		System.out.println(bgson);

		// 将json字符串转换为对象
		Type mtype = new TypeToken<Map<String, Bird>>() {
		}.getType();
		Map<String, Bird> cbirdMap = gson.fromJson(bgson, mtype);
		
		for (Map.Entry<String, Bird> b : cbirdMap.entrySet()) {
			System.out.println(b.getValue().toString());
		}

       打印结果:

       


Map作为成员的类:

/**
 * 定义了鸟类
 */
public class Bird {
	/** 名字 */
	private String BName;
	/** 年龄 */
	private int BAge;
	/** 鸟蛋的属性 */
	private Map<String, BirdEgg> MEgg;

	public Bird() {
		super();
	}

	public Bird(String bName, int bAge) {
		super();
		BName = bName;
		BAge = bAge;
	}

	public Bird(String bName, int bAge, Map<String, BirdEgg> mEgg) {
		super();
		BName = bName;
		BAge = bAge;
		MEgg = mEgg;
	}

	public Map<String, BirdEgg> getMEgg() {
		return MEgg;
	}

	public void setMEgg(Map<String, BirdEgg> mEgg) {
		MEgg = mEgg;
	}

	public String getBName() {
		return BName;
	}

	public void setBName(String bName) {
		BName = bName;
	}

	public int getBAge() {
		return BAge;
	}

	public void setBAge(int bAge) {
		BAge = bAge;
	}

	@Override
	public String toString() {
		return "Bird [BName=" + BName + ", BAge=" + BAge + ", MEgg=" + MEgg
				+ "]";
	}
	
	

}

/**
 * 鸟蛋类
 */
class BirdEgg {
	/** 蛋壳的颜色 */
	private String EColor;
	/** 蛋的重量 */
	private int EWeight;

	public BirdEgg() {
		super();
	}

	public BirdEgg(String eColor, int eWeight) {
		super();
		EColor = eColor;
		EWeight = eWeight;
	}

	public String getEColor() {
		return EColor;
	}

	public void setEColor(String eColor) {
		EColor = eColor;
	}

	public int getEWeight() {
		return EWeight;
	}

	public void setEWeight(int eWeight) {
		EWeight = eWeight;
	}

	@Override
	public String toString() {
		return "BirdEgg [EColor=" + EColor + ", EWeight=" + EWeight + "]";
	}
}

       解析的过程,其实跟解析一般的类相同。

// 获取gson实例
		Gson gson = new Gson();

		Bird abird = new Bird("An", 1);
		BirdEgg egg = new BirdEgg("black", 100);
		Map<String, BirdEgg> mapegg = new HashMap<String, BirdEgg>();
		mapegg.put("egg", egg);
		abird.setMEgg(mapegg);

		// 将对象转换为json字符串
		String bgson = gson.toJson(abird);
		System.out.println(bgson);

		// 将json字符串转换为对象
		Bird bbird = gson.fromJson(bgson, Bird.class);
		System.out.println(bbird.toString());

       打印结果:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值