将数据从JSON文件传输到列表项c#

本文关键字:列表 传输 文件 数据 JSON | 更新日期: 2023-09-27 18:06:37

我想把JSON数据放在一个列表中,向用户显示记录。但是当我运行系统时,列表只显示项目名称两次,而不是JSON数据。

HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("Http://www.eletrotechautomacoes.com.br");
        string url = string.Format("/pontos/consulta.php");
        var response = await client.GetAsync(url);
        var result = response.Content.ReadAsStringAsync().Result;
        List<ExchangeRates> listaProdutos = JsonConvert.DeserializeObject<List<ExchangeRates>>(result);
        this.LlistSpecials.ItemsSource = listaProdutos;

系统在列表中显示的是出现两次的项目名。

文件JSON:

[{"id":"1","nome":"pendrive","preco":"20.00","tipo":"eletronico"},{"id":"2","nome":"Monitor","preco":"250.00","tipo":"eletronicos"}]
对象:

 public class ExchangeRates 
{
    [JsonProperty("id")]
    public string Id { get; set; }
    [JsonProperty("nome")]
    public string Nome { get; set; }
    [JsonProperty("preco")]
    public string Preco { get; set; }
    [JsonProperty("tipo")]
    public string Tipo { get; set; }

}
谢谢你们所有人的帮助。但即便是修正,也不会继续奏效。我认为问题是那部分:this.LlistSpecials.ItemsSource = listaproductos;我的困难是把清单上的内容显示出来。我需要做一些转换到列表显示?他们知道显示这个列表的程序吗?它目前两次返回项目名称。我认为这与JSON项的数量有关,也是两个。

将数据从JSON文件传输到列表项c#

按如下所示更改ExchangeRates类,因为Id将是string而不是list<string>

public class ExchangeRates
{
    public string id { get; set; }
    public string nome { get; set; }
    public string preco { get; set; }
    public string tipo { get; set; }
}

如果你不知道你的模型应该怎么看,你可以使用这个在线生成器

你的模型是这样的:

public class ExchangeRates
    {
        public string id { get; set; }
        public string nome { get; set; }
        public string preco { get; set; }
        public string tipo { get; set; }
    }

更新你的模型,它应该工作。