所以...我得到了JSON文件的以下两个类
public class Scorebot
{
public string Messagedata { get; set; }
public string CurrentScoreCt { get; set; }
public string CurrentScoreT { get; set; }
public string TeamNameCt { get; set; }
public string TeamNameT { get; set; }
public Players[] PlayersCt { get; set; }
public Players[] PlayersT { get; set; }
}
public class Players
{
public string Nickname { get; set; }
public string Weapon { get; set; }
public string Health { get; set; }
public string Armor { get; set; }
public string Money { get; set; }
public string Adr { get; set; }
public string Kills { get; set; }
public string Assists { get; set; }
public string Deaths { get; set; }
}
以下JSON应该看起来像:
{
"messagedata": "kyoto killed SEGADOBR",
"currentscoreCT": "10",
"currentscoreT": "10",
"teamnameCT": "CT",
"teamnameT": "T",
"playersCT": [
{
"Nickname": "SEGADOBR",
"Weapon": "ak47",
"Health": "100",
"Armor": "100",
"Money": "16000",
"ADR": "13.6",
"Kills": "16",
"Assists": "5",
"Deaths": "18"
}
],
"playersT": [
{
"Nickname": "kyoto",
"Weapon": "ak47",
"Health": "100",
"Armor": "100",
"Money": "16000",
"ADR": "13.6",
"Kills": "999",
"Assists": "5",
"Deaths": "18"
}
]
}
我了解的如何访问变量(Messagedata,CurrentsCorect,Currentscoret,TeamNamect和TeamNamet)。但是,我应该做些什么来填补此元素(players [playersct,players [] playert),如何正确访问它们?
据我了解,看起来应该像这样
Scorebot.PlayersCt[i].Nickname = "something"
,但它不起作用。如何做?
您可以这样填充对象:
var scoreBot = new Scorebot
{
Messagedata = "kyoto killed SEGADOBR",
CurrentScoreCt = "10",
CurrentScoreT = "10",
TeamNameCt = "CT",
TeamNameT = "T",
PlayersCt = new Players[]
{
new Players
{
Nickname = "SEGADOBR",
Weapon = "ak47",
Health = "100",
Armor = "100",
Money = "16000",
Adr = "13.6",
Kills = "16",
Assists = "5",
Deaths = "18"
}
},
PlayersT = new Players[]
{
new Players
{
Nickname = "kyoto",
Weapon = "ak47",
Health = "100",
Armor = "100",
Money = "16000",
Adr = "13.6",
Kills = "999",
Assists = "5",
Deaths = "18"
}
}
};
然后,您只需使用Newtonsoft.Json
即可将其转换为JSON:
var jsonScoreBot = JsonConvert.SerializeObject(scoreBot);
Console.WriteLine(jsonScoreBot);
// {"Messagedata":"kyoto killed SEGADOBR","CurrentScoreCt":"10","CurrentScoreT":"10","TeamNameCt":"CT","TeamNameT":"T","PlayersCt":[{"Nickname":"SEGADOBR","Weapon":"ak47","Health":"100","Armor":"100","Money":"16000","Adr":"13.6","Kills":"16","Assists":"5","Deaths":"18"}],"PlayersT":[{"Nickname":"kyoto","Weapon":"ak47","Health":"100","Armor":"100","Money":"16000","Adr":"13.6","Kills":"999","Assists":"5","Deaths":"18"}]}
现在,您只需调用scoreBot.PlayersCt[i].Nickname = "Noone"
即可更新对象。请注意,PlayersCt
中只有一个元素,因此i
必须为0才能避免System.IndexOutOfRangeException
。
我对Scorebot进行了一些更改而不是使用数组,而是使用通用列表。
public class Scorebot
{
public string Messagedata { get; set; }
public string CurrentScoreCt { get; set; }
public string CurrentScoreT { get; set; }
public string TeamNameCt { get; set; }
public string TeamNameT { get; set; }
public List<Players> PlayersCt { get; set; }
public List<Players> PlayersT { get; set; }
}
public class Players
{
public string Nickname { get; set; }
public string Weapon { get; set; }
public string Health { get; set; }
public string Armor { get; set; }
public string Money { get; set; }
public string Adr { get; set; }
public string Kills { get; set; }
public string Assists { get; set; }
public string Deaths { get; set; }
}
然后只创建一个scorebot的对象以及玩家类喜欢
Scorebot objModel = new Scorebot();
List<Players> PlayersCt = new List<Players>();
List<Players> Playersst = new List<Players>();
在这些对象中动态填充您的欲望数据。然后
objModel.PlayersCt = PlayersCt ;
objModel.Playersst = Playersst ;
然后终于只需将该对象转换为
的JSON字符串var jsonScoreBot = JsonConvert.SerializeObject(objModel);