Map sub-property

Asked

Viewed 42 times

0

I have three classes

public class Ligacao
{
    public string Guid { get; set; }
    public string Telefone { get; set; }
    public Status Status { get; set; }
    public string ArquivoJson { get; set; }
    public DateTime DataCriacao { get; set; }
    public DateTime? DataLigacao { get; set; }
    public int CodigoCliente { get; set; }
    public int RequestId { get; set; }
}

public class LigacaoGetResponse
{
    public string Guid { get; set; }
    public System.DateTime DataLigacao { get; set; }
    public LigacaoGet Ligacao { get; set; }
}

public class LigacaoGet
{
    public int Fila { get; set; }
    public string Telefone { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string CPF { get; set; }
    public string CodigoCliente { get; set; }
    public string Ordem { get; set; }
    public int RequestId { get; set; }
    public LigacaoGetCamposAdicionais[] CamposAdicionais { get; set; }
}

The class "Link" is my data source. The classes "Ligacaogetresponse" and "Ligacaoget" will receive the data. I configured the mapping in this way:

Mapper
    .CreateMap<Entities.Ligacao.Ligacao, LigacaoGetResponse>()
    .ForMember(src => src.Ligacao, opt => opt.MapFrom(dest => JsonConvert.DeserializeObject<LigacaoGet>(dest.ArquivoJson)));

Mapper
    .CreateMap<Entities.Ligacao.Ligacao, LigacaoGet>()
    .ForMember(src => src.RequestId, opt => opt.MapFrom(dest => dest.RequestId));

The content of the "Arquivojson" field is a string with a valid json in it. I perform its content in the "Link" field. I have most of the fields in this json. One of the fields I don’t have and need to fill in is the "Requestid" field. This information is in the field of the same name in the "Link" class. As this information is in a "sublevel", I made a second configuration in my mapping. But it doesn’t work!

Json:

{
   "Fila":10079,
   "Telefone":"1199998888",
   "Nome":"Fulano",
   "Email":"[email protected]",
   "CPF":"999.999.999-99",
   "CodigoCliente":"5",
   "Ordem":null,
   "RequestId":0,
   "CamposAdicionais":[
      {
         "Chave":"veiculo",
         "Valor":"FZO-1710"
      },
      {
         "Chave":"url",
         "Valor":"http://www.google.com"
      }
   ]
}
  • Note that the Reuqestid that comes from json is at zero, so I do that second mapping to fill in Requestid, but it doesn’t change the value.

1 answer

2


your class should be something like:

public class LigacaoGet
{
    public int Fila { get; set; }
    public string Telefone { get; set; }
    public string Nome { get; set; }
    public string Email { get; set; }
    public string CPF { get; set; }
    public string CodigoCliente { get; set; }
    public string Ordem { get; set; }
    public int RequestId { get; set; }
    public List<LigacaoCamposAdicionais> CamposAdicionais {get;set;}

}

public class LigacaoCamposAdicionais 
{
    public string Chave {get;set;}
    public string Valor {get;set;}
}

The AutoMapper only used to map classes between classes. O JSON don’t need it. Just use the JsonConvert

string json = "{ \"Fila\":10079, \"Telefone\":\"1199998888\", \"Nome\":\"Fulano\", \"Email\":\"[email protected]\", \"CPF\":\"999.999.999-99\", \"CodigoCliente\":\"5\", \"Ordem\":null, \"RequestId\": 0, \"CamposAdicionais\":[ { \"Chave\":\"veiculo\", \"Valor\":\"FZO-1710\" }, { \"Chave\":\"url\", \"Valor\":\"http://www.google.com\" } ] }";

LigacaoGet obj = JsonConvert.DeserializeObject<LigacaoGet>(json);

Browser other questions tagged

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