change JSON.stringify data

Asked

Viewed 318 times

0

I have a form with several fields when, I have a function that when submitting the form captures the fields:

$('.form').submit(function () {
                var dados = JSON.stringify(jQuery(this).serializeArray());
                alert(dados);
                return false;
            });

the fields returned in data are:

[{"name":"razao_social","value":"INTELIDER"},
{"name":"nome_fantasia","value":"INTELIDER LTDA"},
{"name":"cpf_cnpj","value":"10.999.558/0001-86"},
{"name":"rg_insc_estadual","value":"132456789"},
{"name":"login","value":"gleyson"},
{"name":"senha","value":"123456"},
{"name":"confirma_senha","value":"S"}]"

but I need it to stay that way:

[
  {
      "razao_social": "INTELIDER",
      "nome_fantasia": "INTELIDER LTDA",
      "cpf_cnpj": "10999558000186",
      "rg_insc_estadual": "132456789",
      "usuario":       {
         "login": "gleyson",
         "senha": "123456",
         "ativo": "S"
      },
   }
]

Remembering that I need to remove one password confirmation of the first code and add the field active of the second example, to send this Json.

1 answer

0


You can make the conversion to object:

$('.form').submit(function () {
      var dados = jQuery(this).serializeArray();
      var obj = {};
      $.each(dados, function (i,obj) {
          obj[obj.name] = obj.value;
      });

      obj.usuario = {
            login: obj.login,
            senha: obj.senha,
            ativo: obj.confirma_senha
      };

      var json = JSON.stringify([obj]);
      alert(json);
      return false;
 });

If you prefer you can use the plugin serializeObject from jquery:

$.fn.serializeObject = function() {
    var o = Object.create(null),
        elementMapper = function(element) {
            element.name = $.camelCase(element.name);
            return element;
        },
        appendToResult = function(i, element) {
            var node = o[element.name];

            if ('undefined' != typeof node && node !== null) {
                o[element.name] = node.push ? node.push(element.value) : [node, element.value];
            } else {
                o[element.name] = element.value;
            }
        };

    $.each($.map(this.serializeArray(), elementMapper), appendToResult);
  }

Mode of use:

$('.form').submit(function () {
      var dados = jQuery(this).serializeObject();
      var json = JSON.stringify([dados]);
      alert(json);
      return false;
 });
  • the empty ta return: [{"user":{}}], debugged by Chrome and realized that it does not enter into $.each(data, Function (i,obj) { obj[obj.name] = obj.value; }); , although the variable has content, also has no error.

  • I put an Alert inside this function and saw that it is going through the data array normally, and I see playing the values inside the obj, but I can’t read them in the obj.usuario = { login: obj.login, password: obj.password, active: obj.confirma_password };, they come empty

Browser other questions tagged

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