Home > front end >  How to deserialize a Json with a parent in c#
How to deserialize a Json with a parent in c#

Time:06-10

I have a problem that I can't solve. I need to deserialize a Json. With a Json without parents it works without problems but with the parent "items" it doesn't work, my class is the following:

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; }
}

The api that returns a json of this type:

{"items":
[{
"id":"9",
"nombre":"Fran",
"ape":"",
"email":"[email protected]",
"pass":"example2022",
"foto":"namePhoto.jpeg"
}]
}

and my method always returns a null and I can't find the solution, does anyone know what may be happening:

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); 
       
    }            
}

The response.Content.ReadAsStringAsync(); reads the Json ok, but in the next line the Deserialize returns me NULL.

CodePudding user response:

Fixed thanks to fellow GSerg. I have changed my class to:

 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; }
}

and my method to:

 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");
                }
            }

            
           
        }            
    }
  • Related