How to create a JSON using jquery?

3

How do I create a JSON with several different names?

Same: [{name: "john"}, {name: "pedro"}]

Could someone give me an example?

I tried to do so, but why does it always return the last item twice? Type John in the first and second, Peter twice?

  $(".item-tbody .nome").each(function ()
   {
       obj.nome = $(this).parent().find('.nome').text();

        arrayObj.push(obj);

         nomeArray = JSON.stringify(arrayObj);
          alert(nomeArray);

   });
    
asked by anonymous 14.08.2017 / 16:18

1 answer

2

Your JSON is not valid! Paste it on that site and validate it, your JSON returns:

  

Expecting string or}, not [.

I have created a valid JSON of names for you. Remember that { } = objeto and [ ] = Array . Read more about JSON here .

[{ "nome":"João"},{"nome":"Pedro"}]

obj =
[
   {
      "nome":"João"
   },
   {
      "nome":"Pedro"
   }
]

console.log(obj[0].nome)
console.log(obj[1].nome)
    
14.08.2017 / 16:43