序列化 反序列化-数据库存储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——————————

有收获?希望老铁们来个三连击,给更多的同学看到这篇文章

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值