JAVA中JSON序列化和反序列化

JAVA中JSON序列化和反序列化

  • 1 FASTJSON的序列化和反序列化
    • 1.1 主函数
    • 1.2 自定义对象
  • 2 GSON序列化和反序列化
    • 2.1 主函数
    • 2.2 自定义对象
  • 总结


不同包序列化和反序列化的方式不同,其修改对象属性名称的注释也不相同。

1 FASTJSON的序列化和反序列化

1.1 主函数

public class testJsonMy {

	public static void main(String[] args) {

		String jsonString = "{'id':'1','name':'西游记','prince':36}";
		String jsonStringNew = "{\"code\":\"200\",\"ok\":\"true\",\"data\":[{\"id\":\"3\",\"name\":\"水浒传\",\"prince\":35}]}";

		//1.1使用FastJson获取json字符串中对应对象值
		System.out.println("1.1使用FastJson获取json字符串中对应对象值");
		System.out.println("JSONObject parseObject str: " +jsonString);
		JSONObject jsonObject = JSONObject.parseObject(jsonString);
		String id = jsonObject.getString("id");
		String name = jsonObject.getString("name");
		String prince = jsonObject.getString("prince");
		System.out.println("JSONObject parseObject id: " + id + ", name: " + name + ", prince: " + prince);

		String nameNew = JSONObject.parseObject(jsonString).getString("name");
		System.out.println("JSONObject parseObject nameNew: " + nameNew);


		//1.2使用FastJson转化成自定义类对象, 再获取json字符串中对应对象值
		System.out.println("\n1.2使用FastJson转化成自定义类对象, 再获取json字符串中对应对象值");
		Book book = JSONObject.parseObject(jsonString, Book.class);
		System.out.println("JSONObject parseObject book: " + book);
		System.out.println("JSONObject parseObject book name: " + book.getBookName());

		//1.3使用FastJson转化成Map集合, 再获取json字符串中对应对象值
		System.out.println("\n1.3使用FastJson转化成Map集合, 再获取json字符串中对应对象值");
		Map map = JSONObject.parseObject(jsonString, Map.class);
		System.out.println("JSONObject parseObject map: " + map);
		System.out.println("JSONObject parseObject map name: " + map.get("name"));

		//1.4使用FastJson将对象序列化为json格式的字符串和将字符串反序列化为自定义对象
		System.out.println("\n1.4使用FastJson将对象序列化为json格式的字符串和将字符串反序列化为自定义对象");
		Book book1 = new Book("2", "三国演义", 40);
		String book1JsonString = JSON.toJSONString(book1);
		System.out.println("JSON toJSONString book1JsonString: " + book1JsonString);
		Book book2 = JSONObject.parseObject(book1JsonString, Book.class);
		System.out.println("JSONObject parseObject book2: " + book2);


		//1.5 获取data中内容
		System.out.println("\n1.5 获取data中内容");
		JSONArray data = JSONObject.parseObject(jsonStringNew).getJSONArray("data");
		System.out.println("JSONObject parseObject data: " + data);
		JSONObject jsonObjectData0 = (JSONObject) data.get(0);
		System.out.println("JSONObject parseObject jsonObjectData0: " + jsonObjectData0);
		String name1 = jsonObjectData0.getString("name");
		System.out.println(name1);

	}
}

1.2 自定义对象

使用@JSONField修改序列化和反序列化属性名称。

class Book {
	private String id;

	@JSONField(name = "name") // fastJson
	private String bookName;

	private int prince;

	public Book() {
	}

	public Book(String id, String bookName, int prince) {
		this.id = id;
		this.bookName = bookName;
		this.prince = prince;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public int getPrince() {
		return prince;
	}

	public void setPrince(int prince) {
		this.prince = prince;
	}

	@Override
	public String toString() {
		return "Book{" +
				"id='" + id + '\'' +
				", bookName='" + bookName + '\'' +
				", prince=" + prince +
				'}';
	}
}

2 GSON序列化和反序列化

2.1 主函数

public class testGsonMy {

	private static Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new DateTypeAdapter()).serializeNulls().create();

	public static void main(String[] args) {

		String jsonString = "{'id':'1','name':'西游记','prince':36}";
		String jsonStringNew = "{\"code\":\"200\",\"ok\":\"true\",\"data\":[{\"id\":\"3\",\"name\":\"水浒传\",\"prince\":35}]}";

		System.out.println("\n---------------------------GSON---------------------------");
		System.out.println("gson str: " + jsonString);
		System.out.println("\n2.1使用GSON转化成自定义类对象, 再获取json字符串中对应对象值");
		// 2.1使用GSON转化成自定义类对象, 再获取json字符串中对应对象值
		BookGson bookGson = gson.fromJson(jsonString, BookGson.class);
		System.out.println("gson fromJson bookGson: " + bookGson);
		System.out.println("gson fromJson bookGson name: " + bookGson.getBookName());

		// 2.2使用GSON转化成Map集合, 再获取json字符串中对应对象值
		System.out.println("\n2.2使用GSON转化成Map集合, 再获取json字符串中对应对象值");
		Map bookGsonMap = gson.fromJson(jsonString, Map.class);
		System.out.println("gson fromJson bookGson Map: " + bookGsonMap);
		System.out.println("gson fromJson bookGson Map name: " + bookGsonMap.get("name"));

		//2.3使用GSON将对象序列化为json格式的字符串和将字符串反序列化为自定义对象
		System.out.println("\n2.3使用GSON将对象序列化为json格式的字符串和将字符串反序列化为自定义对象");
		BookGson bookGson1 = new BookGson("3", "水浒传", 42);
		String book1GsonString = gson.toJson(bookGson1);
		System.out.println("gson toString book1GsonString: " + book1GsonString);
		BookGson bookGsonStruct = gson.fromJson(book1GsonString, BookGson.class);
		System.out.println("gson fromJson bookGsonStruct: " + bookGsonStruct);

		//2.4 使用GSON获取data中内容
		//2.4.1 序列化为Map集合, 再获取data中内容
		System.out.println("\n2.4.1 序列化为Map集合, 再获取data中内容");
		Map resultMap = gson.fromJson(jsonStringNew, Map.class);
		System.out.println("gson fromJson resultMap: " + resultMap);
		ArrayList<Map> bookGsons = (ArrayList<Map>) resultMap.get("data");
		System.out.println("bookGsons: " + bookGsons);
		Map bookGsonNew = bookGsons.get(0);
		System.out.println("bookGsonNew: " + bookGsonNew);
		System.out.println("bookGsonNew.name: " + bookGsonNew.get("name"));

		//2.4.2 反序列化为自定义对象, 再获取data中内容
		System.out.println("\n2.4.2 反序列化为自定义对象, 再获取data中内容");
		ResultBook resultBook = gson.fromJson(jsonStringNew, ResultBook.class);
		System.out.println("gson fromJson str: " + jsonStringNew);
		System.out.println("gson fromJson resultBook: " + resultBook);
		System.out.println("gson fromJson bookGsonList: " + resultBook.getBookGsonList());
		System.out.println("gson fromJson bookGsonList[0]: " + resultBook.getBookGsonList().get(0));
		System.out.println("gson fromJson bookGsonList[0].name: " + resultBook.getBookGsonList().get(0).getBookName());

	}
}

2.2 自定义对象

使用@SerializedName修改对象中属性名称

class ResultBook {
	private String code;
	private boolean ok;
	@SerializedName("data") // gson
	private List<BookGson> bookGsonList;

	public ResultBook() {
	}

	public ResultBook(String code, boolean ok, List<BookGson> bookGsonList) {
		this.code = code;
		this.ok = ok;
		this.bookGsonList = bookGsonList;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public boolean isOk() {
		return ok;
	}

	public void setOk(boolean ok) {
		this.ok = ok;
	}

	public List<BookGson> getBookGsonList() {
		return bookGsonList;
	}

	public void setBookGsonList(List<BookGson> bookGsonList) {
		this.bookGsonList = bookGsonList;
	}

	@Override
	public String toString() {
		return "ResultBook{" +
				"code='" + code + '\'' +
				", ok=" + ok +
				", bookGsonList=" + bookGsonList +
				'}';
	}
}


class BookGson {
	private String id;

	@Expose
	@SerializedName("name") // gson
	private String bookName;

	private int prince;

	public BookGson() {
	}

	public BookGson(String id, String bookName, int prince) {
		this.id = id;
		this.bookName = bookName;
		this.prince = prince;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getBookName() {
		return bookName;
	}

	public void setBookName(String bookName) {
		this.bookName = bookName;
	}

	public int getPrince() {
		return prince;
	}

	public void setPrince(int prince) {
		this.prince = prince;
	}

	@Override
	public String toString() {
		return "Book{" +
				"id='" + id + '\'' +
				", bookName='" + bookName + '\'' +
				", prince=" + prince +
				'}';
	}
}


总结

以上就是JSON中序列化和反序列化的主要内容。在序列化和反序列化时可灵活使用注释修改参数名称,便于对参数处理;除此之外,在反序列化时即可以使用Map来获取内容,也可以自定义对象获取内容。

你可能感兴趣的:(JAVA,java)