Error mounting a Json via code

Asked

Viewed 81 times

-1

I’m trying to ride a Json in the following structure:

"contatos":[{"contato":{"sequencia":"2046","codigo":"2046","nome":"teste balizador","tipo_pessoa":"PJ","cpf_cnpj":"32337775000143","ie":"","im":"","rg":"","tipo_negocio":"","endereco":"Rua Três Pontas","numero":"440","complemento":"","bairro":"Aparecida","cidade":"","cep":"30710-560","uf":"","pais":"","contatos":"Tiny","fone":"(31) 2526-0565","fax":"","celular":"","email":"[email protected]","id_vendedor":"4","situacao":"A","obs":""}}]

but when mounting the Json my file is without the contatos:

{"contato":{"sequencia":"2046","codigo":"2046","nome":"teste balizador","tipo_pessoa":"PJ","cpf_cnpj":"32337775000143","ie":"","im":"","rg":"","tipo_negocio":"","endereco":"Rua Três Pontas","numero":"440","complemento":"","bairro":"Aparecida","cidade":"","cep":"30710-560","uf":"","pais":"","contatos":"Tiny","fone":"(31) 2526-0565","fax":"","celular":"","email":"[email protected]","id_vendedor":"4","situacao":"A","obs":""}}

I believe I’m forgetting something I haven’t figured out yet.

follows the excerpt from my code:

public class contato
        {

            public string sequencia { get; set; }
            public string codigo { get; set; }
             ...
            public string obs { get; set; }

        }

        public class Contatos
        {
            public contato contato { get; set; }
        }

 var contato = new contato
            {
                sequencia = cliente.CodCliente.ToString(),
                codigo = cliente.CodCliente.ToString(),
                ...
                obs = cliente.Observacoes
            };

            var contatosList = new Contatos() { contato = contato };
            var contatos = new JavaScriptSerializer().Serialize(contatosList);
  • Your code is missing standardization of class nomenclature. Usually with initial capitalization. are you creating a variable and instantiating itself? var contact = new contact

  • What’s the point of creating a Contacts class from contact?

  • was the way I got to get to the Json pattern, I’m analyzing the code and redoing it more clearly and correctly.

  • Does Your Json have to be in the first structure? Some front rule? In this case it is a Contacts that has a Contact object list. Right?

  • exactly, because it may happen that within contacts have more than one example contact:

  • "contacts":[{"contact":{"sequence":"2046","code":"2046","name":"balizer test","tipo_person":"PJ","cpf_cnpj":"32337775000143","ie":""","im":"","rg":"","tipo_negocio":"""},"contact":{"sequence"": .....}

  • You do not need to run the wheel twice. Your list vc declares like this List<Contact> Contacts = new List<Contact>();

  • Fills contact = new Contact {} and then Contacts.Add(contact);

Show 3 more comments

1 answer

3

Try something like that:

Declares the Contact class. (maintain default of the nomenclature rules)

public class Contato
{

    public string sequencia { get; set; }
    public string codigo { get; set; }
     ...
    public string obs { get; set; }

}

List<Contato> Contatos = new List<Contato>(); // Na sua função vc declara a lista da classe de Contato. No caso, Contatos.

Contato contatoAux = new Contato { // Preenche a Contato.
     sequencia = cliente.CodCliente.ToString(),
     codigo = cliente.CodCliente.ToString(),
     ...
     obs = cliente.Observacoes
};

Contatos.Add(contatoAux); // adiciona na lista.


// obviamente passou de uma inserção você irá utilizar um foreach. 
contatoAux = new Contato {
     sequencia = cliente2.CodCliente.ToString(),
     codigo = cliente2.CodCliente.ToString(),
     ...
     obs = cliente2.Observacoes
};

Contatos.Add(contatoAux);
  • Thank you Rebecca, I understood perfectly, but after serializing it was not in the standard and the post was not realized, as it was later: [{"sequence":"2052","code":"2052"..."Obs":""}] and the pattern is this: "contacts"[{ "contact": {"sequence":"2052","code":"2052","..."." Obs":""}}]

  • How you did the serialization?

  • good morning!! desa forma: var contacts = new Javascriptserializer(). Serialize(Contacts);

  • Give me your code and how it runs, it can be here https://dotnetfiddle.net/ Create it and save and send me the link

  • I think this will help you. http://www.macoratti.net/16/07/c_jsonob1.htm

  • sorry for the delay. I had other urgent demands here. I will create.

  • I read the article, I used the same method and still stumble on the assembly of Json: follows the excerpt that makes the post:

  • List<Contact> Contacts = new List<Contact>(); Contact = new Contact { sequence = client.CodCliente.Tostring(), }; Contacts.Add(contact);

  • var contatos = JsonConvert.SerializeObject(Contatos.ToList(), Formatting.Indented);&#xA; var stringContent = new StringContent(contatos, Encoding.UTF8, "application/json");&#xA; var result = await new HttpClient(). Postasync($"https://api.tiny.com.br/api2/contact.inclu.php/authorize?token={token}&contact={contacts}&format=json", stringContent); string return = await result.Content.Readasstringasync();

  • @Gustavo.NET Put the code in some executable like dotnetfiddle.net so I can simulate, just like that, I can’t help. srsrsrsr Or call me on social networks that you give me the code rebecanonato89

  • I called on instagram.

  • morning follow the link: https://dotnetfiddle.net/iZX8Ou

Show 7 more comments

Browser other questions tagged

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