Insert an array into a Key Json

Asked

Viewed 96 times

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"
        }
    ]
}

The email would have to be an array?

What the code for that would be?

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 = ???
    ...            
}
  • 1

    Yes. That is the question?

  • Also, I’d like to know how you would implement this?

  • Implement what? You just played a JSON on the question =)

  • How would code kkkkk sorry XD

  • 1

    Young man, what would the code be for? Create JSON? Do you already have the classes ready? What do you have ready?

  • I edited the post with my code, need to know how I will pass the values in emails and phones

Show 1 more comment

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 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 is important to note that you are not following the C nomenclature standards#. I also find it interesting that you rename the class Telefones for Telefone (see that I used this pattern in my answer) because the class represents a phone number and not several. Already the property, should be called Telefones (plural), I think you ended up reversing the concepts.

Another important thing: if you will only use one property (as it shows in the classes Email and Telefones), it would be easier to directly create a property like List<string> in its 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; }
  • Ah yes! Got it! Mt thank you!!

  • I get it! But for example, I need to send this json via http, the application that receives json, by default has to go the way I wrote it (Emails and Phones), setting it as List<string> would not interfere with the result of Json no?

  • It would change yes, because the property emails there would be no objects inside it, but an array of strings

  • Got it! Thanks for your help!

  • Always at your disposal =).

Browser other questions tagged

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