Map sub-property

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 "Connection" is my data source. The "ConnectionGetResponse" and "ConnectionGet" classes will receive the data. I configured the mapping 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 contents of the "jsonfile" field is a string with a valid json inside it. I underealise your content in the "Connection" field. I have most of the fields in this json. One of the fields I do not have and need to fill in is the "RequestId" field. This information is in the field of the same name in the "Connection" class. Since this information is in a "sub-level", I made a second configuration in my mapping. But it does not 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.br"
      }
   ]
}
    
asked by anonymous 12.04.2018 / 22:31

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 uses to map classes between classes. JSON does not need it. Just use 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.br\" } ] }";

LigacaoGet obj = JsonConvert.DeserializeObject<LigacaoGet>(json);
    
13.04.2018 / 14:35