0

Hello Stackers,

i have a json file and i want to get the values randomly and divide the result in two. the idea is to divide the list of 14 players on two means 7 players in each team and the selection must be randomly presented

Here is the Json file located in Models Folder

{
    "Football": {
        "Players": [
            {
                "id": "1",
                "Name": "Gabriel"
            },
            {
                "id": "2",
                "Name": "Leozin"            
            },
            {
                "id": "3",
                "Name": "Gustavinho"
            },
            {
                "id": "4",
                "Name": "Bira"
            },
            {
                "id": "5",
                "Name": "Alvin"
            },
            {
                "id": "6",
                "Name": "Sugar"
            },
            {
                "id": "7",
                "Name": "Donato"
            },
            {
                "id": "8",
                "Name": "Alessandro"
            },
            {
                "id": "9",
                "Name": "Kenji"
            },
            {
                "id": "10",
                "Name": "Kleber"
            },
            {
                "id": "11",
                "Name": "Lemão"
            },
            {
                "id": "12",
                "Name": "Jonatas"
            },
            {
                "id": "13",
                "Name": "Abdo"
            },
            {
                "id": "14",
                "Name": "Kleber"
            }
        ]
    }
}

Here is the Controller:

using System.Net.Http;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace MidasFootballApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET api/values
        [HttpGet]
        public object Get()
        {
            string path = @"Models/Football.json";
            string allText = System.IO.File.ReadAllText(path);
            object jsonObject = JsonConvert.DeserializeObject(allText);
            return  jsonObject;
        }
    }
}

Any help will be appreciated

Abdessamad Jadid
  • 381
  • 1
  • 4
  • 16
  • 2
    I'd suggest to create a `Player` class first, then convert you data to `List` then pick items from this list, to get a random number see e.g. https://stackoverflow.com/questions/2706500/how-do-i-generate-a-random-int-number – Christoph Lütjen Sep 10 '20 at 20:58

1 Answers1

1

Here's a quick sample i wipped up - it's not good code, and you need to adapt it to your controller still but something i quickly made before going to sleep. I think you can now figure out the rest and cleanup the sample.

using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Linq;

public class Program
{
    public static string jsonStr = "{\"Football\": {\"Players\": [{\"id\": \"1\",\"Name\": \"Gabriel\"},{\"id\": \"2\",\"Name\": \"Leozin\"},{\"id\": \"3\",\"Name\": \"Gustavinho\"},{\"id\": \"4\",\"Name\": \"Bira\"},{\"id\": \"5\",\"Name\": \"Alvin\"},{\"id\": \"6\",\"Name\": \"Sugar\"},{\"id\": \"7\",\"Name\": \"Donato\"},{\"id\": \"8\",\"Name\": \"Alessandro\"},{\"id\": \"9\",\"Name\": \"Kenji\"},{\"id\": \"10\",\"Name\": \"Kleber\"},{\"id\": \"11\",\"Name\": \"Lem\u00E3o\"},{\"id\": \"12\",\"Name\": \"Jonatas\"},{\"id\": \"13\",\"Name\": \"Abdo\"},{\"id\": \"14\",\"Name\": \"Kleber\"}]}}";
    
    public static void Main()
    {
        var jsonObject = JsonConvert.DeserializeObject<FootballJson>(jsonStr);
        var listOfLines = jsonObject.Football.Players.ToList();
        
        var genRandoms = new Random();
        var numberRequired = listOfLines.Count / 2;
        var output = new List<Player>();
        for (var i = 0; i < numberRequired; i++)
        {
            var aRandomTeam = genRandoms.Next(listOfLines.Count);
            output.Add(listOfLines[aRandomTeam]);
            listOfLines.RemoveAt(aRandomTeam);
        }
        
        foreach(var item in listOfLines)
            Console.WriteLine(item.Name);
        
        Console.WriteLine("\n> Second: ");
        
        foreach(var item in output)
            Console.WriteLine(item.Name);
    }
}


public partial class FootballJson
{
    [JsonProperty("Football")]
    public Football Football
    {
        get;
        set;
    }
}

public partial class Football
{
    [JsonProperty("Players")]
    public List<Player> Players
    {
        get;
        set;
    }
}

public partial class Player
{
    [JsonProperty("id")]
    public string Id
    {
        get;
        set;
    }

    [JsonProperty("Name")]
    public string Name
    {
        get;
        set;
    }
}

Leozin
Gustavinho
Alvin
Sugar
Donato
Kenji
Abdo

> Second: 
Jonatas
Kleber
Bira
Kleber
Gabriel
Alessandro
Lemão

https://dotnetfiddle.net/lDmyeb

Resources: https://stackoverflow.com/a/58117884/4122889

sommmen
  • 6,570
  • 2
  • 30
  • 51