将数据从JSON文件传输到列表项c#
我想把JSON数据放在一个列表中,向用户显示记录。但是当我运行系统时,列表只显示两次项目名称,而不显示JSON数据将数据从JSON文件传输到列表项c#,c#,json,list,rest,C#,Json,List,Rest,我想把JSON数据放在一个列表中,向用户显示记录。但是当我运行系统时,列表只显示两次项目名称,而不显示JSON数据 HttpClient client = new HttpClient(); client.BaseAddress = new Uri("Http://www.eletrotechautomacoes.com.br"); string url = string.Format("/pontos/consulta.php"); var re
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;
对象:
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=listaProdutos;我的困难是显示列表中的内容。我需要对要显示的列表进行一些转换?他们知道显示此列表的程序吗?它当前两次返回项目名称。我认为这与JSON项的数量有关,JSON项也有两个。更改您的
ExchangeRates
类,如下所示,因为Id
将是一个string
而不是列表
更改您的ExchangeRates
类,如下所示,因为Id
将是字符串
而不是列表
如果你不知道你的模特应该看起来怎么样,你可以用这个
您的模型如下所示:
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; }
}
更新你的模型,它应该可以工作。@AnuradhS为什么会有帮助,如何帮助?HttpClient不是WebClient的继承者吗?它是新开发中使用的首选类吗?为什么您的属性id
isList
?由于您的json,它可能是string
,然后可以淡化。如果您将id
定义为List
,那么json应该是“id”:[“1”]
等等。@AnuradhS为什么会有帮助?HttpClient不是WebClient的继承者吗?它是新开发中使用的首选类吗?为什么您的属性id
isList
?由于您的json,它可能是string
,然后可以淡化。如果您将id
定义为List
,那么json应该是“id”:[“1”]
等。感谢您的帮助。但即使是应用修正也不会继续。我认为问题在于:this.LlistSpecials.ItemsSource=listaProdutos;我的困难是显示列表中的内容。我需要对要显示的列表进行一些转换?他们知道显示此列表的程序吗?它当前两次返回项目名称。我认为这与JSON项目的数量有关,这也是两个项目。谢谢大家的帮助。但即使是应用修正也不会继续。我认为问题在于:this.LlistSpecials.ItemsSource=listaProdutos;我的困难是显示列表中的内容。我需要对要显示的列表进行一些转换?他们知道显示此列表的程序吗?它当前两次返回项目名称。我认为这与JSON项的数量有关,这也是两个。
public class ExchangeRates
{
public string id { get; set; }
public string nome { get; set; }
public string preco { get; set; }
public string tipo { get; set; }
}