JSON PARSE returning 'object object'

3

I would like to know what is wrong with my function, because by assigning obj = data , it saves object object , the date value being a JSON returned by WEBSERVICE.

Date value:

[{"descricao":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tristique \nligula sed nibh finibus, id placerat elit mollis. Proin dictum sed felis vitae vulputate. Nunc vitae velit feugiat, porttitor \nfelis quis, pharetra risus. Integer id dolor sapien. Aenean ultricies, risus at lacinia blandit, leo mi tincidunt risus, \nat gravida nisi nisl ut dui. Donec vitae consectetur urna. Phasellus at augue vel nisl semper aliquet eu vel ante. Ut venenatis \nlacus id velit aliquet, ac congue neque mollis. Quisque at elit mollis arcu condimentum imperdiet.","idEvento":3,"inicio":"05/10","palestrante":"Profº José Marinho","qtdVagas":40,"termino":"09/10","titulo":"Palestra de Jogos"}]

JavaScript Function:

$(document).ready(function(){
    $(".btnSubmit").click(function(evt) {
        evt.preventDefault();

        $.ajax({
            type: "GET",
            url: "http://localhost:8080/SemanaEngenharia/webresources/services.evento",
            data: "", /* redundante */
            dataType: "text",
            success: function(data) {
                alert(data);
                obj = JSON.parse(data); /* nao funciona*/
                //$("#p").html(obj.titulo);
                alert(obj);
            }

        }); // fim ajax

    });
});

When doing obj.titulo (one of the JSON fields), the return is "undefined".

    
asked by anonymous 07.12.2018 / 15:34

2 answers

4

alert does not display the contents of the object. If you do a console.log will show. As an array object, you access values by index 0, since it is an array of only one JSON item, for example:

obj[0].descricao;

It will access the value of the key descricao .

Now you can change dataType to json so that the string is already parsed, without the need to use JSON.parse :

   $.ajax({
     type: "GET",
     url: "conecta2.php",
     data: "", /* redundante */
     dataType: "json",
     success: function(data) {
         alert(data);
         //$("#p").html(obj.titulo);
         console.log(data[0].descricao);
     }

   }); // fim ajax
    
07.12.2018 / 15:55
3

Your JSON is an array (since it is delimited by [ ] ), and inside this array there is an object (delimited by { } ), who owns the "title" key.

Then just get the first element of the array, and then get the "title":

let json = [{"descricao":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In tristique \nligula sed nibh finibus, id placerat elit mollis. Proin dictum sed felis vitae vulputate. Nunc vitae velit feugiat, porttitor \nfelis quis, pharetra risus. Integer id dolor sapien. Aenean ultricies, risus at lacinia blandit, leo mi tincidunt risus, \nat gravida nisi nisl ut dui. Donec vitae consectetur urna. Phasellus at augue vel nisl semper aliquet eu vel ante. Ut venenatis \nlacus id velit aliquet, ac congue neque mollis. Quisque at elit mollis arcu condimentum imperdiet.","idEvento":3,"inicio":"05/10","palestrante":"Profº José Marinho","qtdVagas":40,"termino":"09/10","titulo":"Palestra de Jogos"}]

console.log(json[0].titulo); // Paletras de Jogos
    
07.12.2018 / 15:54