0

So... I got these two following classes for json file

    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; }
    } 

the following json should looking like:

{
  "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"
            }
  ]     
}

How to access variables (Messagedata, CurrentScoreCt, CurrentScoreT, TeamNameCt and TeamNameT) i understood. But what i should do to fill elements in this (Players[] PlayersCt, Players[] PlayersT), how to access them correctly?

As i understand it should look like this

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

But it didn't work. How to do it?

Nicerok11
  • 13
  • 4
  • 1
    `But it didn't work.` what does it mean? What issue you are facing ? Are you trying to create json from class object or create object from json? – Chetan Mar 16 '19 at 06:22
  • When i'm starting program i getting `System.NullReferenceException: Object reference not set to an instance of an object.` with following code `Scorebot.PlayersT = new Players[5]; Scorebot.PlayersCt = new Players[5]; for (var i = 0; i < 5; i++) { Scorebot.PlayersCt[i].Nickname = "Noone"; } for (var i = 0; i < 5; i++) { Scorebot.PlayersT[i].Nickname = "Noone"; }` – Nicerok11 Mar 16 '19 at 06:24
  • 1
    @Nicerok1 and have you initialized any of those variables/arrays? I'm guessing not. Reference types will default to `null` until you assign values to them. – ProgrammingLlama Mar 16 '19 at 06:26
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – ProgrammingLlama Mar 16 '19 at 06:26
  • I'm trying to create json from class "Scorebot" and it worked until the moment I started trying to fill the array PlayersT and PlayersCt – Nicerok11 Mar 16 '19 at 06:26
  • @Nicerok11 We know you are, but you need to initialize classes. Otherwise you'll be trying to serialize nothng, which is clearly not possible. – ProgrammingLlama Mar 16 '19 at 06:28
  • And did you initialize `PlayersT`? – ProgrammingLlama Mar 16 '19 at 06:32
  • i initialized them like this `Scorebot.PlayersT = new Players[5]; Scorebot.PlayersCt = new Players[5];` isn't this correct? – Nicerok11 Mar 16 '19 at 06:33
  • That initializes the array, so you have an array of 5 `null` values. You can then initialize each index of the array to contain an object (e.g. `Scorebot.PlayersCt[0] = new Players();`) – ProgrammingLlama Mar 16 '19 at 06:34
  • And then im trying to fill elements to not get null reference exception `for (var i = 0; i < 5; i++) { Scorebot.PlayersCt[i].Nickname = "Noone"; } for (var i = 0; i < 5; i++) { Scorebot.PlayersT[i].Nickname = "Noone"; }` – Nicerok11 Mar 16 '19 at 06:35
  • Search for _"Example: Array of class objects"_ in the linked "What is a NullReferenceException" duplicate. Essentially you create 5 boxes, and then try to take something out of them, but you only have boxes, you don't have anything inside them. You have to put something inside the box before you can take it out. – ProgrammingLlama Mar 16 '19 at 06:36
  • You need to do `Scorebot.PlayersCt[i] = new Players();` before `Scorebot.PlayersCt[i].Nickname = "Noone";` – Chetan Mar 16 '19 at 06:46

2 Answers2

1

You can fill in your object like this:

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"
        }
    }
};

Then you can simply convert it to JSON using Newtonsoft.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"}]}

And now you can simply call scoreBot.PlayersCt[i].Nickname = "Noone" to update the object as well. Note that only one element exists in PlayersCt, so i must be 0 to avoid a System.IndexOutOfRangeException.

RoadRunner
  • 25,803
  • 6
  • 42
  • 75
  • Thanks! But i still have some questions. I will write you an email so can you help me in your free time? – Nicerok11 Mar 16 '19 at 07:21
  • @Nicerok11 You can ask your questions here. I'm happy to assist you more, as long as its still relevant to the question. – RoadRunner Mar 16 '19 at 12:23
0

I made a few changes in Scorebot instead of using an array, use a generic list.

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; }
    } 

then just create an object of Scorebot as well player class like

Scorebot objModel = new Scorebot();
List<Players> PlayersCt = new List<Players>();
List<Players> Playersst = new List<Players>();

Fill your desire data dynamically in those objects. then

objModel.PlayersCt = PlayersCt ;
objModel.Playersst = Playersst ;

then at last just convert that object into a JSON string like

var jsonScoreBot = JsonConvert.SerializeObject(objModel);
Dark S
  • 308
  • 2
  • 15
  • Thanks! But how i supposed to fill data into that list? So i need to create a new player in list and then fill properties(nickname, etc.)? – Nicerok11 Mar 16 '19 at 08:33