当前位置: 首页 > 工具软件 > Gson > 使用案例 >

解析json之com.google.gson.Gson

慎兴业
2023-12-01

需要导入的架包

gson-2.2.4.jar

JSON与数组互转

package com.itlwc.text;

import com.google.gson.Gson;

public class Test {
	public static void main(String[] args) {
		//将数组转JSON
		int[] number = {1,2,3,4,5};
        Gson gson = new Gson();  
        String numberjson = gson.toJson(number);
        System.out.println(numberjson);
        
        //将JSON转数组
        int[] numberNew = gson.fromJson(numberjson, int[].class);
        for (int i = 0; i < numberNew.length; i++) {
            System.out.print(numberNew[i] + " ");
        }
        System.out.println("");
	}
}

JSON与JavaBean互转

package com.itlwc.text;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

class Student {
	private String no;
	private String name;
	private List<String> firends;

	public Student(String no, String name, List<String> firends) {
		this.no = no;
		this.name = name;
		this.firends = firends;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<String> getFirends() {
		return firends;
	}

	public void setFirends(List<String> firends) {
		this.firends = firends;
	}

	@Override
	public String toString() {
		return "学号: " + this.getNo() + " 姓名:" + this.getName() + " 朋友:"
				+ this.getFirends();
	}
}

public class Test {
	public static void main(String[] args) {
		// JavaBean转JSON
		List list = new ArrayList();
		list.add("1002");
		list.add("1003");
		Student stu = new Student("1001", "lwc", list);
		Gson gson = new Gson();
		String jsondata = gson.toJson(stu);
		System.out.println(jsondata);

		// JSON转JavaBean
		stu = null;
		stu = gson.fromJson(jsondata, Student.class);
		System.out.println(stu);
	}
}
/*
打印结果: 
	{"no":"1001","name":"lwc","firends":["1002","1003"]}
	学号: 1001 姓名:lwc 朋友:[1002, 1003]
*/

JSON与List互转

package com.itlwc.text;

import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;

public class Test {
	public static void main(String[] args) {
		// List转JSON
		List list = new ArrayList();
		for (int i = 0; i < 5; i++) {
			list.add("element" + i);
		}
		System.out.println(list);
		Gson gson = new Gson();
		String json = gson.toJson(list);
		System.out.println(json);

		// JSON转List
		List list2 = (List) gson.fromJson(json, Object.class);
		for (int i = 0; i < list2.size(); i++) {
			System.out.println((String) list2.get(i));
		}
	}
}
/*
打印结果:
	[element0, element1, element2, element3, element4]
	["element0","element1","element2","element3","element4"]
	element0
	element1
	element2
	element3
	element4
*/

JSON与泛型List互转

package com.itlwc.text;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

class Student {
	private String no;
	private String name;

	public Student(String no, String name) {
		this.no = no;
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "学号: " + this.getNo() + " 姓名:" + this.getName();
	}
}

public class Test {
	public static void main(String[] args) {
		// 泛型Lis转JSON
		List<Student> list = new ArrayList<Student>();
		for (int i = 0; i < 3; i++) {
			Student stu = new Student("100" + i, "name" + i);
			list.add(stu);
		}
		System.out.println(list);
		Type type = new TypeToken<List<Student>>() {}.getType();
		Gson gson = new Gson();
		String json = gson.toJson(list, type);
		System.out.println(json);
		
		//JSON转泛型Lis
		List<Student> users = gson.fromJson(json.toString(), type);
	    System.out.println(users);
	}
}
/*
打印结果:
	[学号: 1000 姓名:name0, 学号: 1001 姓名:name1, 学号: 1002 姓名:name2]
	[
		{"no":"1000","name":"name0"},{"no":"1001","name":"name1"},
		{"no":"1002","name":"name2"}
	]
	[学号: 1000 姓名:name0, 学号: 1001 姓名:name1, 学号: 1002 姓名:name2]
*/

Map转JSON

package com.itlwc.text;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;

public class Test {
	public static void main(String[] args) {
		// Map转JSON
		Map map = new HashMap();
		map.put("no", "1001");
		map.put("name", "lwc");
		System.out.println(map);
		Gson gson = new Gson();
		String json = gson.toJson(map);
		System.out.println(json);
	}
}
/*
打印结果:
	{no=1001, name=lwc}
	{"no":"1001","name":"lwc"}
*/

JSON与泛型Map互转

package com.itlwc.text;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

class Student {
	private String no;
	private String name;

	public Student(String no, String name) {
		this.no = no;
		this.name = name;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "学号: " + this.getNo() + " 姓名:" + this.getName();
	}
}

public class Test {
	public static void main(String[] args) {
		// JSON转泛型Map
		Map<String, Student> map = new HashMap<String, Student>();
		for (int i = 0; i < 2; i++) {
			Student stu = new Student("100" + i, "name" + i);
			map.put("100" + i, stu);
		}
		System.out.println(map);
		Type type = new TypeToken<Map<String, Student>>() {
		}.getType();
		Gson gson = new Gson();
		String json = gson.toJson(map, type);
		System.out.println(json);

		// 转泛型Map转JSON
		Map<String, Student> mapStu = gson.fromJson(json.toString(), type);
		System.out.println(mapStu);
	}
}
/*
打印结果:
	{1001=学号: 1001 姓名:name1, 1000=学号: 1000 姓名:name0}
	{"1001":{"no":"1001","name":"name1"},"1000":{"no":"1000","name":"name0"}}
	{1001=学号: 1001 姓名:name1, 1000=学号: 1000 姓名:name0}
*/

JSON与带日期JavaBean互转

package com.itlwc.text;

import java.lang.reflect.Type;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

class Student {
	private String no;
	private String name;
	private Date date;

	public Student(String no, String name, Date date) {
		this.no = no;
		this.name = name;
		this.date = date;
	}

	public String getNo() {
		return no;
	}

	public void setNo(String no) {
		this.no = no;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public String toString() {
		return "学号: " + this.getNo() + " 姓名:" + this.getName() + " 入学时间:"
				+ this.getDate();
	}
}

/**
 * 日期解序列实用工具类
 * 
 * @author Administrator
 * 
 */
class DateSerializerUtils implements JsonSerializer<java.util.Date> {
	public JsonElement serialize(Date arg0, Type arg1,
			JsonSerializationContext arg2) {
		return new JsonPrimitive(arg0.getTime());
	}

}

/**
 * 日期序列化实用工具类
 * 
 * @author Administrator
 * 
 */
class DateDeserializerUtils implements JsonDeserializer<java.util.Date> {
	public Date deserialize(JsonElement arg0, Type arg1,
			JsonDeserializationContext arg2) throws JsonParseException {
		return new java.util.Date(arg0.getAsJsonPrimitive().getAsLong());
	}
}

public class Test {
	public static void main(String[] args) {
		// 带日期JavaBean转JSON
		Student stu = new Student("1001", "lwc", new Date());
		Gson gson = new Gson();
		gson = new GsonBuilder().registerTypeAdapter(Date.class,
				new DateSerializerUtils()).setDateFormat(DateFormat.LONG)
				.create();
		String json = gson.toJson(stu);
		System.out.println(json);

		// JSON转带日期JavaBean
		gson = new GsonBuilder().registerTypeAdapter(Date.class,
				new DateDeserializerUtils()).setDateFormat(DateFormat.LONG)
				.create();
		Type type = new TypeToken<Student>() {
		}.getType();
		Student s = gson.fromJson(json, type);
		System.out.println(s);

		
		
		
		// 泛型日期List转JSON
		List<Student> list = new ArrayList<Student>();
		list.add(new Student("1001", "name1", new Date()));
		list.add(new Student("1002", "name2", new Date()));
		System.out.println(list);
		gson = new GsonBuilder().registerTypeAdapter(Date.class,
				new DateSerializerUtils()).setDateFormat(DateFormat.LONG)
				.create();
		Type type2 = new TypeToken<List<Student>>() {
		}.getType();
		json = gson.toJson(list, type2);
		System.out.println(json);

		// JSON转泛型日期List
		gson = new GsonBuilder().registerTypeAdapter(Date.class,
				new DateDeserializerUtils()).setDateFormat(DateFormat.LONG)
				.create();
		List<Student> stus = gson.fromJson(json.toString(), type);
		System.out.println(stus);
	}
}
/*
打印结果: 
	{"no":"1001","name":"lwc","date":1407561795000}
	学号: 1001 姓名:lwc 入学时间:Sat Aug 09 13:23:15 CST 2014
	[学号: 1001 姓名:name1 入学时间:Sat Aug 09 13:23:15 CST 2014, 学号: 1002 姓名:name2 入学时间:Sat Aug 09 13:23:15 CST 2014]
	[{"no":"1001","name":"name1","date":1407561795093},{"no":"1002","name":"name2","date":1407561795093}]
*/

 类似资料: