List part of a JSON file using C#

Asked

Viewed 159 times

-1

For reasons of errors with a Windows version I ended up not working with databases and started using files JSON. But I need to upload only part of the items (List) to one DataGridView according to the "type" I select in a ComboBox. For this I am using the conditional loop switch:

public class LotofacilQuantidades
{       
    public int CONC { get; set; }
    public string DATA { get; set; }
    public int SOMA { get; set; }
    public int qPAR { get; set; }
    public string PAR { get; set; }
    public int qIMP { get; set; }
    public string IMPAR { get; set; }
    public int qREP { get; set; }
    public string REPETIDO { get; set; }
    public string INICIAL { get; set; }
    public string FINAL { get; set; }
    public int qFIB { get; set; }
    public string FIBONACCI { get; set; }
    public int qNP { get; set; }
    public string PRIMOS { get; set; }
    public int qINP { get; set; }
    public string INP { get; set; }
    public int qMD { get; set; }
    public string MOLDURA { get; set; }
    public int qSQ { get; set; }
    public string SEQUENCIA { get; set; }
    public int qITV { get; set; }
    public string INTERVALO { get; set; }
    public int qX { get; set; }
    public string XIS { get; set; }
    public int qL01 { get; set; }
    public string LINHA_01 { get; set; }
    public int qL02 { get; set; }
    public string LINHA_02 { get; set; }
    public int qL03 { get; set; }
    public string LINHA_03 { get; set; }
    public int qL04 { get; set; }
    public string LINHA_04 { get; set; }
    public int qL05 { get; set; }
    public string LINHA_05 { get; set; }
    public int qC01 { get; set; }
    public string COLUNA_01 { get; set; }
    public int qC02 { get; set; }
    public string COLUNA_02 { get; set; }
    public int qC03 { get; set; }
    public string COLUNA_03 { get; set; }
    public int qC04 { get; set; }
    public string COLUNA_04 { get; set; }
    public int qC05 { get; set; }
    public string COLUNA_05 { get; set; }
}

What I need is to deserialize the file JSON taking only the "CONC", "DATA" + the type that was determined in the ComboBox and its corresponding amount. Ex: case : "ODD" it would bring me the list with "CONC", "DATA", "ODD", "qIMP". Something like:

 //busco o arquivo json
 var _arq = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Relatorio.json");

 //listo o arquivo
 List<LotofacilQuantidades> _lstFinal = JsonConvert.DeserializeObject<List<LotofacilQuantidades>>(_arq);
 //seleciono os itens que pretendo utilizar com o switch/case (aqui está meu erro/problema)
 var teste1 = _lstFinal.Select(c => c.CONC && c.DATA && c.FINAL).ToList();

In this case it is only allowed to list an item with Select.

  • 1

    1- the OS is not a forum. 2- go to [Tour] to understand 3- just vc do an if and return the select according to the condition q vc need... and do not need to read the json file every time... the select should be... .Select(c => new { c.CONC, c.DATA, c.Final });

  • OK Rovann. Thank you so much for your help.

1 answer

1


I made an example using a combobox, and when changing the combo is performed select:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == 0)
    {
        dataGridView1.DataSource = lista.Select(x => new { x.Nome, x.Propriedade1 }).ToList();
    }
    else if (comboBox1.SelectedIndex == 1)
    {
        dataGridView1.DataSource = lista.Select(x => new { x.Nome, x.Propriedade2 }).ToList();
    }
    else if (comboBox1.SelectedIndex == 2)
    {
        dataGridView1.DataSource = lista.Select(x => new { x.Nome, x.Propriedade3 }).ToList();
    }
    else
        dataGridView1.DataSource = null;
}

Model Class:

public class Modelo
{
    public string Nome { get; set; }
    public int Propriedade1 { get; set; }
    public int Propriedade2 { get; set; }
    public int Propriedade3 { get; set; }
}

Note: datagridview is to generate columns automatically.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.