Insert an array inside a Key Json

2

I need to get a Json as follows:

{
    "razao_social": "Loja do Zé LTDA",
    "nome_fantasia": "Zé Store",
    "tipo": "J",
    "observacao": "Cliente com ótimo histórico de pagamentos.",
    "emails": [
        {
            "email": "[email protected]"
        },
        {
            "email": "[email protected]"
        }
    ],
    "telefones": [
        {
            "numero": "(11) 98765-4321"
        },
        {
            "numero": "(47) 9876-5432"
        }
    ]
}

Should email be an array?

What would be the code for this?

My Class

public class GetClienteBO
{
    public int id { get; set; }
    public string razao_social { get; set; }
    public string nome_fantasia { get; set; }
    public string tipo { get; set; }
    public string cnpj { get; set; }
    public string Inscricao_estadual { get; set; }
    public string suframa { get; set; }
    public string rua { get; set; }
    public string complemento { get; set; }
    public string cep { get; set; }
    public string bairro { get; set; }
    public string cidade { get; set; }
    public string estado { get; set; }
    public string observacao { get; set; }
    public Email emails { get; set; }
    public Telefones telefone { get; set; }
    public bool excluido { get; set; }
}

public class Email
{
    public string email { get; set; }
}

public class Telefones
{
    public string numero { get; set; }
}

Code:

[WebMethod]
[ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public void JsonTeste(string JsonChamada)
{
    SerializerFO Serializer = new SerializerFO();
    var Retorno = new GetClienteBO();

    Retorno.id = 1;
    Retorno.razao_social = "Loja do Zé LTDA";
    Retorno.nome_fantasia = "Zé Store";
    Retorno.tipo = "J";
    ...

    Retorno.estado = "SC";
    Retorno.cidade = "Joinville";
    Retorno.excluido = true;
    Retorno.observacao = "Cliente com ótimo histórico de pagamentos.";
    Retorno.emails = ????
    Retorno.telefones = ???
    ...            
}
    
asked by anonymous 10.06.2016 / 19:52

1 answer

1

Yes, it is possible.

In class GetClienteBO , change

public Email emails { get; set; }
public Telefones telefone { get; set; }

for

public List<Email> emails { get; set; }
public List<Telefone> telefones { get; set; }

So instead of the properties receiving only one instance of Email / Telefone , they will receive collections of these types.

In the assignment method, do

Retorno.emails = new List<Email> 
                 { 
                     new Email { email = "[email protected]" },
                     new Email { email = "[email protected]" },
                 };
Retorno.telefones = new List<Telefone> 
                    { 
                        new Telefone { numero = "30005000" },
                        new Telefone { numero = "88996654" } 
                    };

It's important to note that you're not following the C # . I also find it interesting that you rename the class Telefones to Telefone (see I used this pattern in my answer) because the class represents one phone number and not several. Since the property should be called Telefones (in the plural), I think you ended up reversing the concepts.

Another important thing: If you are going to use only one property (as shown in the Email and Telefones classes), it would be easier to directly create a property like List<string> in your main class. Of course this depends on all the other details of your application, this is just a tip.

Something like:

public List<string> Emails { get; set; }
public List<string> Telefones { get; set; }
    
10.06.2016 / 20:18