JSON PARSE returning 'Object Object'

Asked

Viewed 1,446 times

2

I would like to know what is wrong with my job, because in allocating obj = data, the same sage object object, the date value is a JSON returned by the 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

    });
});

In doing obj.titulo (one of the JSON fields), the return is "Undefined".

  • Have you tried to define the dataType as JSON to not need to do the conversion manually?

  • If there are brackets it is because it is converting one array of objects. If you do not need the array, convert the object directly. Without seeing the code behind all this it is impossible to say.

  • I found the bug. On my webservice, it returns a list of all events, so the brackets. How could I change this? I’m using JPA

  • Search in the documentation what the dataType makes you understand.

  • my method in java: https://imgur.com/a/7MYuQcT it returns a list, so the brackets and why parse json is not working. Would have some possibility to change the structure of this JPA without using list?

  • In case you change Java to return a different structure, I suggest that ask another question specifically about this (just be sure to do a search before to see if this question already exists on the site).

Show 1 more comment

2 answers

3

Your JSON is an array (as it is bounded by [ ]), and within this array there is a Object (delimited by { }), who holds the key "title".

Then just take the first element of the array, and then take 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

  • This group saves my college in an amazing way. Thank you so much for your help!!!

  • @Weslleyfillipe Don’t forget to choose one of the answers (if she has solved your problem) and accept it (see in the FAQ how to do it). It is not obligatory, of course, but it is considered good practice of the site to indicate to future visitors that it solved the problem. In this case, only one of the answers can be accepted, so if you decide to accept one of them, choose the one that best solved the problem.

  • 1

    Ready! Thanks again!

3


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

obj[0].descricao;

Will access the key value descricao.

Now you can change the dataType for 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

Browser other questions tagged

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