封装
// 存储json数据战斗记录
JsonObject jsonObj = new JsonObject();
uint battleTime = ServerTimeManager.Instance.GetServerCurTimeStamp();
jsonObj["battleTime"] = new JsonProperty(battleTime);
string uuid = string.Empty;
jsonObj["uuid"] = new JsonProperty(uuid);
string name = string.Empty;
jsonObj["name"] = new JsonProperty(name);
int level = 0;
jsonObj["level"] = new JsonProperty(level);
int avatar = 0;
jsonObj["avatar"] = new JsonProperty(avatar);
int score = 0;
jsonObj["score"] = new JsonProperty(score);
int isWin = result;
jsonObj["isWin"] = new JsonProperty(isWin);
List<Card> myCards = ClientDungeonData.Instance.TeamCards;
List<JsonProperty> Items = new List<JsonProperty>();
for (int i = 0; i < myCards.Count; i++)
{
JsonObject obj = new JsonObject();
obj["id"] = new JsonProperty(myCards[i].ID);
obj["level"] = new JsonProperty(myCards[i].Level);
JsonProperty property = new JsonProperty(obj);
Items.Add(property);
}
jsonObj["cards"] = new JsonProperty(Items);
battleRecord = jsonObj.ToString();
JsonObject aaa = new JsonObject(battleRecord);
JsonProperty action = aaa["cards"];
List<Ladder.LadderRecordData.CardInfo> mCardInfos = new List<Ladder.LadderRecordData.CardInfo>();
for (int i = 0; i < action.Items.Count; i++)
{
Ladder.LadderRecordData.CardInfo info = new Ladder.LadderRecordData.CardInfo();
JsonObject obj = action.Items[i].Object;
JsonProperty property = obj["id"];
info.ID = (int)property.Number;
property = obj["level"];
info.Level = (int)property.Number;
mCardInfos.Add(info);
}
数据格式
public class CardInfo
{
public int ID;
public int Level;
}
private uint mBattleTime;
private string mUuid;
private string mName;
private int mLevel;
private int mAvatar;
private int mScore;
private bool mIsWin;
private List<CardInfo> mCardInfos;
解析
public LadderRecordData(string record)
{
JsonObject jsonObj = new JsonObject(record);
JsonProperty action = jsonObj["battleTime"];
mBattleTime = (uint)action.Number;
action = jsonObj["uuid"];
mUuid = action.Value;
action = jsonObj["name"];
mName = action.Value;
action = jsonObj["level"];
mLevel = (int)action.Number;
action = jsonObj["avatar"];
mAvatar = (int)action.Number;
action = jsonObj["score"];
mScore = (int)action.Number;
action = jsonObj["isWin"];
mIsWin = (int)action.Number == 1;
action = jsonObj["cards"];
mCardInfos = new List<CardInfo>();
for (int i = 0; i < action.Items.Count; i++)
{
CardInfo info = new CardInfo();
JsonObject obj = action.Items[i].Object;
JsonProperty property = obj["id"];
info.ID = (int)property.Number;
property = obj["level"];
info.Level = (int)property.Number;
mCardInfos.Add(info);
}
}
网友评论