0

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":"example@gmail.com",
"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.

Fran Pino
  • 111
  • 7
  • have you tried to add the property 'id' to your Item class ? – Romka Jun 07 '22 at 09:22
  • 5
    You've declared a single `T Items`. You have a list of them in JSON. Although it's not really JSON because it contains [single quotes](https://stackoverflow.com/q/36038454/11683). – GSerg Jun 07 '22 at 09:22
  • 1
    What's returned isn't valid JSON as "items" isn't contained within an object. Unfortunately, you'll have to clean it up before you can deserialize it. – ProgrammingLlama Jun 07 '22 at 09:24
  • The JSON is malformed, as @DiplomacyNotWar has said, it must start with {. – Ricardo Peres Jun 07 '22 at 09:33
  • When pasting the json code I missed including {, but the Api returns the Json between {} – Fran Pino Jun 07 '22 at 09:54
  • The Json is exactly {"items":[{"id":"9","nombre":"Frank","ape":"","email":"userfran@gmail.com","pass":"franexample22","foto":"namePhoto.jpeg"}]} with double quotes, and still returns Null – Fran Pino Jun 07 '22 at 10:06
  • 1
    Because you [still](https://stackoverflow.com/questions/72528768/how-to-deserialize-a-json-with-a-parent-in-c-sharp#comment128121848_72528768) have a single `T Items` in your `Root`, and a list of items in json. – GSerg Jun 07 '22 at 10:36

1 Answers1

0

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

            
           
        }            
    }
Fran Pino
  • 111
  • 7