0

I have this segment of a JSON file:

[
    {"Name": "Chelsea",
     "GoalDiff": 20,
     "Points": 25
    },
    {"Name": "Liverpool",
     "GoalDiff": 30,
     "Points": 35
    },
    {"Name": "Man United",
     "GoalDiff": 5,
     "Points": 18
    }

]

I need to put these into an array per object, where I can easily access them to sort, use as variables, and put into a formula.

EDIT:

I have tried some things from this site to just read the file, with little success for example

class Program
    {
        public class TeamInfo
        {
            public string Name {get; set;} 
            public int GoalDiff {get; set;}
            public int points { get; set; }
        }
        public void LoadJson()
        {
            using(StreamReader r = new StreamReader("TXT1.json"))
            {
                string json = r.ReadToEnd();
                List<TeamInfo> items = JsonConvert.DeserializeObject<List<TeamInfo>>(json);
            }
        }
        static void Main(string[] args)
        {
            dynamic array = JsonConvert.DeserializeObject(json);
            foreach (var item in array)
            {
                Console.WriteLine(item.temp, item.vcc)
            }

        }

    }

Similarly the newtonsoft website didn't help me a whole load.

Redacted
  • 1
  • 2
  • 2
    OK. What is your question? – nbokmans Dec 17 '19 at 12:20
  • 1
    Whad did you try so far ? Do you have any source code to share –  Dec 17 '19 at 12:21
  • Sounds like you need a class such as `Team` with properties of `Name`, `GoalDiff` and `Points`, then just deserialize your JSON to a list or array of that class. – Jon Skeet Dec 17 '19 at 12:22
  • Does this answer your question? [Deserialize JSON with C#](https://stackoverflow.com/questions/7895105/deserialize-json-with-c-sharp) – haldo Dec 17 '19 at 12:31
  • If you're using .Net Core 3.0 or above my [answer](https://stackoverflow.com/a/59009748/9665021) to the linked question should help – haldo Dec 17 '19 at 12:39

1 Answers1

1

First create a class like this.

public class FootballTeams
{
    public string Name { get; set; }
    public int GoalDiff { get; set; }
    public int Points { get; set; }
}

Then read the data and use Newtonsoft.Json package

var json = File.ReadAllText(@"data.json");
var footballTeams = JsonConvert.DeserializeObject<List<FootballTeams>>(json);

// Do something with the footballTeams Eg : footballTeams.Select(team=>team.Name.Equals("Chelsea"));

HariHaran
  • 3,642
  • 2
  • 16
  • 33
  • Would there be anything further required to add into an array, or is this getting rid of that and suggesting using linq to sort ect – Redacted Dec 17 '19 at 12:26