Jsonproperty(Propertyname) does not recognize and assign values to the object

Asked

Viewed 123 times

1

I’m creating a API, POST method, where the payload consists of 3 classes with the following structure:

//Classe principal do payload
public class Representante
{
    [JsonProperty(PropertyName = "representatives")]
    public List<Representante> Representantes { get; set; }
}

//Classe que compõe o tipo da lista acima
[JsonObject(Title = "Representative")]
public class Representante
{
    [JsonProperty(PropertyName = "personCode")]
    public string CodigoPessoa { get; set; }


    [JsonProperty(PropertyName = "cpf")]
    public string Cpf { get; set; }


    [JsonProperty(PropertyName = "name")]
    public string Nome { get; set; }


    [JsonProperty(PropertyName = "clientPart")]
    public ClienteParte ClienteParte { get; set; }
 }

//Classe referente ao objeto que compõe a classe 'Representante'
[JsonObject(Title = "clientPart")]
public class ClienteParte
{
    [JsonProperty(PropertyName = "name")]
    public string Nome { get; set; }


    [JsonProperty(PropertyName = "numberDocument")]
    public string NumeroDocumento { get; set; }
}

When performing a test to validate whether everything I reported on payload below reflects on the input objects, I noticed that the object ClienteParte ("clientPart") is returned with null value. I need to inform a different structure in Propertyname to understand that the property belongs to one class within another classe? Example: [JsonProperty(PropertyName="clientPart.name")]

Payload:

{
"sellerCode":332,
"representatives": [ {
          "personCode": 41333440,
          "cpf": 67788947687,
          "name": "TAPIXU ENMU FIHJAE",
          "signatureType": "C",
          "clientPart": [{
               "name":" JOAO DA SILVA",
               "numberDocument ":"67826789372"
          }],
}]
}

Return reflected in input class:

Printscreen retorno API

  • 1

    I think you just got a little confused, look at the json, the clientpart is in brackets, I mean, it’s a list, in your Representative class it’s just an object

  • clientPart is poorly formed, has a left comma and key numberDocument has a space inside between the t and the ".

  • The error was in the same brackets. I fixed it here and it worked. Thank you!

1 answer

0

The ClientPart is a array (a list), so your type model is wrong, should be as in the example below:

public class Representante
{
    [JsonPropertyName("sellerCode")]
    public int SellerCode { get; set; }

    [JsonPropertyName("representatives")]
    public Representative[] Representatives { get; set; }
}

public class Representative
{
    [JsonPropertyName("personCode")]
    public int PersonCode { get; set; }

    [JsonPropertyName("cpf")]
    public long CPF { get; set; }

    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("signatureType")]
    public string SignatureType { get; set; }

    [JsonPropertyName("clientPart")]
    public Clientpart[] ClientPart { get; set; }
}

public class Clientpart
{
    [JsonPropertyName("name")]
    public string Name { get; set; }

    [JsonPropertyName("numberDocument")]
    public string NumberDocument { get; set; }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.