Estou com um problema que não consigo resolver. Eu preciso desserializar um Json. Com um Json sem pais funciona sem problemas mas com os "itens" pais não funciona, minha classe é a seguinte:
public partial class Root<T>
{
[JsonProperty("items")]
public T Items { get; set; }
}
public partial class Item
{
[JsonProperty("nombre")]
public string Nombre { get; set; }
[JsonProperty("ape")]
public string Ape { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("pass")]
public string Pass { get; set; }
[JsonProperty("foto")]
public string Foto { get; set; }
}
A api que retorna um json desse tipo:
{"items":
[{
"id":"9",
"nombre":"Fran",
"ape":"",
"email":"[email protected]",
"pass":"example2022",
"foto":"namePhoto.jpeg"
}]
}
e meu método sempre retorna um null e não consigo achar a solução, alguém sabe o que pode estar acontecendo:
public async Task EmailApiGet(string pass)
{
var request = new HttpRequestMessage();
request.RequestUri = new System.Uri("https://app.adress.com/movil/rest/index.php?email=" + Email.Value);
request.Method = HttpMethod.Get;
request.Headers.Add("Accept", "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string json = await response.Content.ReadAsStringAsync();
Root<Item> name = JsonConvert.DeserializeObject<Root<Item>>(json); //Test
Console.WriteLine(name.Items.Nombre);
}
}
O response.Content.ReadAsStringAsync();
lê o Json ok, mas na próxima linha o Deserialize me retorna NULL .
Corrigido graças ao colega GSerg. Mudei minha classe para:
public partial class Root
{
[JsonProperty("items")]
public Item[] Items { get; set; }
}
public partial class Item
{
[JsonProperty("id")]
[JsonConverter(typeof(ParseStringConverter))]
public long Id { get; set; }
[JsonProperty("nombre")]
public string Nombre { get; set; }
[JsonProperty("ape")]
public string Ape { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
[JsonProperty("pass")]
public string Pass { get; set; }
[JsonProperty("foto")]
public string Foto { get; set; }
}
e meu método para:
public async Task EmailApiGet(string pass)
{
var request = new HttpRequestMessage();
request.RequestUri = new System.Uri("https://app.example.com/example/rest/index.php?email=" + Email.Value);
request.Method = HttpMethod.Get;
request.Headers.Add("Accept", "application/json");
var client = new HttpClient();
HttpResponseMessage response = await client.SendAsync(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string json = await response.Content.ReadAsStringAsync();
Root name = JsonConvert.DeserializeObject<Root>(json); //Deserializo el json para quedarme sólo con el pass
foreach(var i in name.Items)
{
if (pass == i.Pass)
{
Console.Write("Contraseña CORRECTA");
displayNotication("Correcto");
}
else
{
Console.Write("Contraseña INCORRECTA");
displayNotication("Incorrecto");
}
}
}
}
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras