using System;
using UnityEngine;
/*
编写一个json的数据文档,内部存储了一组玩家的所有信息,每一个玩家的信息如下:
ID编号, Name姓名, Descript个人介绍, VipDetail会员信息
每一个会员的信息如下:
Level等级,Title称号,Exp经验值
使用序列化方法 打印出json字符串,并将该json字符串反序列化为对象
*/
[Serializable]
public class PlayerMsgs
{
public PlayerMsg[] _playerMsg;
}
[Serializable]
public class PlayerMsg
{
public int num;
public string name;
public string descripts;
public VipMsg _VipMsgs;
public PlayerMsg(int num, string name, string descripts,VipMsg vipMsg)
{
this.num = num;
this.name = name;
this.descripts = descripts;
_VipMsgs = vipMsg;
}
}
[Serializable]
public class VipMsg
{
public int grade;
public string title;
public int exp;
public VipMsg(int grade, string title, int exp)
{
this.grade = grade;
this.title = title;
this.exp = exp;
}
}
public class JsonWork : MonoBehaviour
{
private void Start()
{
VipMsg v1=new VipMsg(1,"无敌的我",90);
VipMsg v2=new VipMsg(2,"漂亮的我",81);
VipMsg v3=new VipMsg(3,"帅气的我",72);
VipMsg v4=new VipMsg(4,"好看的我",63);
VipMsg v5=new VipMsg(5,"幽默的我",54);
VipMsg v6=new VipMsg(6,"爱玩的我",55);
PlayerMsg p1=new PlayerMsg(01,"小明","我很帅",v1);
PlayerMsg p2=new PlayerMsg(02,"小红","我很强",v2);
PlayerMsg p3=new PlayerMsg(03,"小刚","我很靓",v3);
PlayerMsg p4=new PlayerMsg(04,"小强","我很好",v4);
PlayerMsg p5=new PlayerMsg(05,"小丽","我很坏",v5);
PlayerMsg p6=new PlayerMsg(06,"小亮","我很牛",v6);
PlayerMsgs msg=new PlayerMsgs();
msg._playerMsg = new PlayerMsg[6] {p1, p2, p3, p4, p5, p6};
string str = JsonUtility.ToJson(msg);
Debug.Log(str);
PlayerMsgs msg1 = JsonUtility.FromJson<PlayerMsgs>(str);
foreach (PlayerMsg item in msg1._playerMsg)
{
Debug.Log("编号:"+item.num+"\n姓名:"+item.name+"\n自我介绍:"+item.descripts+ "\n会员等级:"
+item._VipMsgs.grade+ "\n会员称号:"+item._VipMsgs.title+"\n会员经验:"+item._VipMsgs.exp);
}
}
}
|