为JSON创建和填充数据

时间:2019-03-16 06:17:28

标签: c# arrays json class json.net

所以...我得到了以下两个用于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 [] PlayersT),如何正确访问它们?

据我了解,它应该是这样

Scorebot.PlayersCt[i].Nickname = "something"

但是没有用。怎么做?

2 个答案:

答案 0 :(得分:1)

您可以这样填充对象:

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

答案 1 :(得分:0)

我在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);