How to insert values inside a key using List and JSON

Asked

Viewed 598 times

0

I’m trying to do something that gives me a way out of this:

[
    {
        "Nome": "João",
        "Comprou": [
                       "Carro": "Sedan", "Preco": "12000",
                       "Moto": "Honda", "Preco": "8000"
                   ]
    }
]

For this I use List and to save use JSON.net. The problem is that I am not able to put one key inside another. I tried this way:

public class Cliente
{
    public string Nome { get; set; }
    public string[] Comprou { get; set; }
}
public static List<Cliente> Clientes = new List<Cliente>();

I don’t know how to assign one value within another. I want to take the products and put them in a listbox for the particular customer who is selected in another listbox. I tried using foreach:

Cliente cliente = Clientes[listaClientes.SelectedIndex];
foreach (var produto in cliente.Comprou)
{
        listaProdutosComprados.Items.Add(produto);
}

I wanted to put the values as "Name" and "Price" as I did with the client so that I could display these values in a label or textbox. I am developing this project in C# with a Winforms application.

  • Hello! Welcome to SOPT. Which library is used to serialize/deserialize?

  • @Omni Hello! Thank you for being interested in helping me solve my problem. I am using JSON.net

1 answer

3

You need to expand a little more your pro JSON.Net template to make the correct interpretation:

public class Cliente
{
    public string Nome { get; set; }
    public List<Veiculo> Comprou { get; set; }
}

public class Veiculo
{
    public string Carro { get; set; }  
    public string Moto { get; set; }
    public Decimal Preco { get; set; }
}

You can instantiate like this:

var clientes = new List<Cliente> {
    Nome = "João", 
    Comprou = new List<Veiculo> {
        new Veiculo {
            Carro = "Sedan",
            Preco = 12000
        },
        new Veiculo {
            Moto = "Honda",
            Preco = 8000
        }
    }
};

To serialize in JSON:

string output = JsonConvert.SerializeObject(clientes);

Source: http://james.newtonking.com/json/help/index.html

  • Thank you for your reply! The problem is that my system works like this: I register a client and it is added in a listbox made for customers. When I select a customer in the listbox I can register a product for him through my other listbox for the products. What I need is that when registering the product, the name of the product that is in a textbox goes to the string "Name" and the value of the product that is in another textbox go to the "Price" of the respective selected customer.

  • So you have to assemble the classes as I put the answer there in your Code Behind. It doesn’t come ready from ASPX.

  • I would need to relate one class to another to then do something like textbox1.Text = cliente.Comprou.Veiculo.Nome The name of the vehicle in case would be the one selected in the list of products. Therefore the Cliente cliente = Clientes[listaClientes.SelectedIndex]; Unfortunately I’m not knowing how to do this. I’m trying all day.

  • Well, then the question is no longer about JSON, but about how you relate information to each other. It would be more something to do with Web Forms + Javascript.

  • The JSON I use to save the data and later load it when starting the program. For example, when starting the file it is deserialized. The customer name is added to the customer’s listbox as well as added to the Customer List with its other information. The same goes for products. What I need is to relate a product to the customer. I’m developing this in C# in a Winforms application.

  • as you commented just above, the code snippet cliente.Comprou.Veiculo.Nome should not work because the property comprouis a list or an array. the right one would be cliente.Comprou[0].Veiculo.Nome or something like that. And by your doubt, I think she’s either poorly formulated or her logic is having some problems.

  • @Richarddias I find it hard to explain. I need to register the product that is selected in a listbox for the customer that is selected in another listbox. The product name, as well as the value, I pick up by the text of a textbox and assign to cliente.Comprou[listaClientes.SelectedIndex].Veiculo.Nome = caixaNomeVeiculo.Text and to the cliente.Comprou[listaClientes.SelectedIndex].Veiculo.Preco = caixaPrecoVeiculo.Text

  • But by your logic, the above comment is incorrect. should be cliente.Comprou[listaProdutos.SelectedIndex].Veiculo.Nome = caixaNomeVeiculo.Text. Anyway, I think it would be good if you put doubt in a visual way on paper, far away in the IDE to assemble a logic. You said you are using windows Forms, you are doing all this operation inside a component webBrowser? This is really necessary?

  • @Richarddias a listbox has to be the customer’s because the product must be added to it. I’m developing this in a winforms application in Visual C#.

Show 4 more comments

Browser other questions tagged

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