序列化 反序列化-數據庫存儲json

序列化和反序列化作用(存儲數據)

純手工原創,講得不夠詳細,湊合着看吧,不嫌棄的話可以點個贊回個評論哦,有什麼不足的地方請指出,我會虛心學習的,謝謝

  • 主要內容
  • 序列化
  • 反序列化

主要內容

最近在做一個手機棋牌遊戲的項目,我負責的是java後臺這一塊,中間就遇到一個這樣的問題---

遊戲回放的json數據要存儲到mysql
而一開始我是這樣做的,直接把C++ POST過來的json數據用List< string > 來接收,然後轉成 string字符串寫進mysql裏遊戲回放字段(varchar類型),
遊戲回放數據json格式:(主要部分)

[{
		"UserData_t": [{
			"Chu_Pai_Count": 0,
			"Gang_Pai_Count": 0,
			"HandPai": [4, 5, 6, 7, 8, 9, 11, 13, 14, 15, 16, 17, 18, 11],
			"Hand_Pai_Count": 14,
			"LaiZi_Count": 0,
			"Men_Pai_Count": 28,
			"Peng_Pai_Count": 0,
			"m_BeiShu": 1,
			"m_Final_Score": 0,
			"m_Part_Score": 0,
			"m_Player_ID": 101000,
			"m_Player_Location": 0
		}, {
			"Chu_Pai_Count": 0,
			"Gang_Pai_Count": 0,
			"HandPai": [1, 2, 4, 2, 2, 2, 6, 23, 24, 25, 26, 27, 28],
			"Hand_Pai_Count": 13,
			"LaiZi_Count": 0,
			"Men_Pai_Count": 26,
			"Peng_Pai_Count": 0,
			"m_BeiShu": 1,
			"m_Final_Score": 0,
			"m_Part_Score": 0,
			"m_Player_ID": 102000,
			"m_Player_Location": 1
		}, {
			"Chu_Pai_Count": 0,
			"Gang_Pai_Count": 0,
			"HandPai": [29, 16, 2, 4, 4, 5, 6, 7, 8, 9, 11, 12, 13],
			"Hand_Pai_Count": 13,
			"LaiZi_Count": 0,
			"Men_Pai_Count": 28,
			"Peng_Pai_Count": 0,
			"m_BeiShu": 1,
			"m_Final_Score": 0,
			"m_Part_Score": 0,
			"m_Player_ID": 103000,
			"m_Player_Location": 2
		}, {
			"Chu_Pai_Count": 0,
			"Gang_Pai_Count": 0,
			"HandPai": [14, 6, 16, 4, 4, 19, 21, 22, 23, 24, 25, 26, 27],
			"Hand_Pai_Count": 13,
			"LaiZi_Count": 0,
			"Men_Pai_Count": 26,
			"Peng_Pai_Count": 0,
			"m_BeiShu": 1,
			"m_Final_Score": 0,
			"m_Part_Score": 0,
			"m_Player_ID": 104000,
			"m_Player_Location": 3
		}],
		"m_ChaoTian": 19,
		"m_Dice_1": 6,
		"m_Dice_2": 2,
		"m_LaiZi": 11,
		"m_MenPai_EndIndex": 1,
		"m_MenPai_EndLoc": 1,
		"m_MenPai_LaiziIndex": 3,
		"m_MenPai_LaiziLoc": 1,
		"m_MenPai_StartIndex": 4,
		"m_MenPai_StartLoc": 3,
		"m_MenPai_ZhuangIndex": 2,
		"m_MenPai_ZhuangLoc": 1,
		"m_Zhuang_Location": 0
	}, {
		"Action": 1,
		"ChuPai": [19, 4],
		"NewHandPai": [5, 6, 7, 8, 9, 11, 11, 13, 14, 15, 16, 17, 18],
		"m_ChuPai": 4,
		"m_Player_Location": 0,
		"m_User_ID": 101000
	}, {
		"Action": 2,
		"NewHandPai": [2, 5, 6, 7, 8, 9, 11, 12, 13, 16, 29],
		"PengPai": [4],
		"m_Desk_ID": 120434,
		"m_PengPai": 4,
		"m_Player_Location": 2,
		"m_User_ID": 103000
	}, {
		"Action": 12,
		"ChuPai": [19],
		"NewHandPai": [5, 6, 7, 8, 9, 11, 11, 13, 14, 15, 16, 17, 18],
		"m_Desk_ID": 120434,
		"m_PengPai": 4,
		"m_Player_Location": 0,
		"m_User_ID": 101000
	}, {
		"Action": 1,
		"ChuPai": [2],
		"NewHandPai": [5, 6, 7, 8, 9, 11, 12, 13, 16, 29],
		"PengPai": [4],
		"m_ChuPai": 2,
		"m_Player_Location": 2,
		"m_User_ID": 103000
	}, {
		"Action": 0,
		"MenPaiCount": [28, 21, 0, 4],
		"NewHandPai": [4, 4, 6, 14, 16, 19, 21, 21, 22, 23, 24, 25, 26, 27],
		"m_GangPai_Count": 0,
		"m_Player_Loacation": 3,
		"m_User_ID": 104000,
		"m_ZhuaPai": 21
	}, {
		"Action": 11
	}]
	

後來用api調試工具postman獲取到的數據:(主要部分)

[{
		UserData_t=[{
			Chu_Pai_Count=0,
			Gang_Pai_Count=0,
			HandPai=[4,5,6,7,8,9,11,13,14,15,16,17,18,11],
			Hand_Pai_Count=14,
			LaiZi_Count=0,
			Men_Pai_Count=28,
			Peng_Pai_Count=0,
			m_BeiShu=1,
			m_Final_Score=0,
			m_Part_Score=0,
			m_Player_ID=101000,
			m_Player_Location=0
		},{
			Chu_Pai_Count=0,
			Gang_Pai_Count=0,
			HandPai=[1,2,4,2,2,2,6,23,24,25,26,27,28],
			Hand_Pai_Count=13,
			LaiZi_Count=0,
			Men_Pai_Count=26,
			Peng_Pai_Count=0,
			m_BeiShu=1,
			m_Final_Score=0,
			m_Part_Score=0,
			m_Player_ID=102000,
			m_Player_Location=1
		},{
			Chu_Pai_Count=0,
			Gang_Pai_Count=0,
			HandPai=[29,16,2,4,4,5,6,7,8,9,11,12,13],
			Hand_Pai_Count=13,
			LaiZi_Count=0,
			Men_Pai_Count=28,
			Peng_Pai_Count=0,
			m_BeiShu=1,
			m_Final_Score=0,
			m_Part_Score=0,
			m_Player_ID=103000,
			m_Player_Location=2
		},{
			Chu_Pai_Count=0,
			Gang_Pai_Count=0,
			HandPai=[14,6,16,4,4,19,21,22,23,24,25,26,27],
			Hand_Pai_Count=13,
			LaiZi_Count=0,
			Men_Pai_Count=26,
			Peng_Pai_Count=0,
			m_BeiShu=1,
			m_Final_Score=0,
			m_Part_Score=0,
			m_Player_ID=104000,
			m_Player_Location=3
		}],
		m_ChaoTian=19,
		m_Dice_1=6,
		m_Dice_2=2,
		m_LaiZi=11,
		m_MenPai_EndIndex=1,
		m_MenPai_EndLoc=1,
		m_MenPai_LaiziIndex=3,
		m_MenPai_LaiziLoc=1,
		m_MenPai_StartIndex=4,
		m_MenPai_StartLoc=3,
		m_MenPai_ZhuangIndex=2,
		m_MenPai_ZhuangLoc=1,
		m_Zhuang_Location=0
	},{
		Action=1,
		ChuPai=[19,4],
		NewHandPai=[5,6,7,8,9,11,11,13,14,15,16,17,18],
		m_ChuPai=4,
		m_Player_Location=0,
		m_User_ID=101000
	},{
		Action=2,
		NewHandPai=[2,5,6,7,8,9,11,12,13,16,29],
		PengPai=[4],
		m_Desk_ID=120434,
		m_PengPai=4,
		m_Player_Location=2,
		m_User_ID=103000
	},{
		Action=12,
		ChuPai=[19],
		NewHandPai=[5,6,7,8,9,11,11,13,14,15,16,17,18],
		m_Desk_ID=120434,
		m_PengPai=4,
		m_Player_Location=0,
		m_User_ID=101000
	},{
		Action=1,
		ChuPai=[2],
		NewHandPai=[5,6,7,8,9,11,12,13,16,29],
		PengPai=[4],
		m_ChuPai=2,
		m_Player_Location=2,
		m_User_ID=103000
	},{
		Action=0,
		MenPaiCount=[28,21,0,4],
		NewHandPai=[4,4,6,14,16,19,21,21,22,23,24,25,26,27],
		m_GangPai_Count=0,
		m_Player_Loacation=3,
		m_User_ID=104000,
		m_ZhuaPai=21
	},{
		Action=11
	}]

我們可以看到前後的區別,前者的 “ 消失了 和 :變成了 後者的=,顯而易見,數據發生的變化,後者的數據無法被客戶端正常解析或解析難度大(C++ 服務端 通過post java後端 把數據寫進 mysql數據庫,app客戶端 通過post請求獲取 數據 ,然後解析數據 做成遊戲回放 ) ,嗯,這樣存數據是行不通的。

後來我找到的解決方法,就是先把json數據序列化後再存進mysql數據庫,此時數據庫字段類型應改爲blob類型(底層byte[]存儲),
當客戶端獲取時,後臺從mysql數據庫獲取到json數據,再把json數據反序列化後再返回給客戶端。

Hutool 一個小而全的Java工具類庫(推薦使用);

序列化

			//方法一
			Object gamePlayback = (Object) map.get("json");
			ByteArrayOutputStream out = new ByteArrayOutputStream();
            ObjectOutputStream outputStream = new ObjectOutputStream(out);
            outputStream.writeObject(gamePlayback);
            byte[] bytes = out.toByteArray();
            outputStream.close();
			//方法二
			// Hutool工具類庫
			import cn.hutool.core.util.ObjectUtil;
			Object gamePlayback = (Object) map.get("json");
            byte[] bytes = ObjectUtil.unserialize(gamePlayback);

最後把bytes存數據庫

反序列化

			//方法一
			ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
			ObjectInputStream in = new ObjectInputStream(byteArrayInputStream);
			Object gamePlayback = (Object) in.readObject();
	        in.close();  
			//方法二
			// Hutool工具類庫
			import cn.hutool.core.util.ObjectUtil;
			Object gamePlayback = ObjectUtil.serialize(bytes)

最後把gamePlayback 返回給app客戶端

數據如下:(配圖)

這裏寫圖片描述

end——————————

有收穫?希望老鐵們來個三連擊,給更多的同學看到這篇文章

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章