How to print JSON object list

Asked

Viewed 928 times

0

I’m implementing the facebook posts of a particular page on my site and I’m having trouble understanding how to print the following JSON structure:

{
  "posts": {
    "data": [
      {
        "created_time": "2017-11-16T19:52:28+0000",
        "message": "Teste | JAGUAFRANGOS | A Agência produziu os layouts, vídeo institucional e toda a cobertura do desfile em comemoração ao aniversário de 70 anos de Jaguapitã e aos 25 anos da JAGUAFRANGOS na cidade. ",
        "story": "Teste",
        "id": "157428127680859_1519494691474189"
      }
    ],
    "paging": {
      "cursors": {
        "before": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUWTJPREU1T1Rrek1UazROemMxTXprek1USVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UazBPVFEyT1RFME56UXhPRGtQQkhScGJXVUdXZAzNzZAkFFPQZDZD",
        "after": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUYzVOemM0TWpZAMU9EYzJNVGsyTXpjMk16RVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UQXdNemM1TURVM05UTXlNREVQQkhScGJXVUdXZA0lOOWdFPQZDZD"
      },
      "next": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUYzVOemM0TWpZAMU9EYzJNVGsyTXpjMk16RVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UQXdNemM1TURVM05UTXlNREVQQkhScGJXVUdXZA0lOOWdFPQZDZD"
    }
  },
  "id": "123456789101112113"
}

I tried this code but could not print on the screen the list of objects:

$(document).ready(function(){
    $.getJSON( "posts.txt", function(data) {
        console.log('loaded');
        var data = []; // create array here
        $.each(data.posts, function (index, posts) {
            data.push(posts.data); //push values here
        });
        console.log(data); // see the output here
    });
});
  • What appears on the screen?

  • Nothing really. It was supposed to appear on the console but still could not get the desired list. Returns me only "[ ]"

  • Is there an error in the console? And you have two variables with the same name data

1 answer

2


To print the list you can use the following function:

var obj = {
    "posts": {
        "data": [
            {
                "created_time": "2017-11-16T19:52:28+0000",
                "message": "Teste | JAGUAFRANGOS | A Agência produziu os layouts, vídeo institucional e toda a cobertura do desfile em comemoração ao aniversário de 70 anos de Jaguapitã e aos 25 anos da JAGUAFRANGOS na cidade. ",
                "story": "Teste",
                "id": "157428127680859_1519494691474189"
            }
        ],
        "paging": {
            "cursors": {
                "before": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUWTJPREU1T1Rrek1UazROemMxTXprek1USVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UazBPVFEyT1RFME56UXhPRGtQQkhScGJXVUdXZAzNzZAkFFPQZDZD",
                "after": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUYzVOemM0TWpZAMU9EYzJNVGsyTXpjMk16RVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UQXdNemM1TURVM05UTXlNREVQQkhScGJXVUdXZA0lOOWdFPQZDZD"
            },
            "next": "Q2c4U1pXNTBYM0YxWlhKNVgzTjBiM0o1WDJsa0R5UXhOVGMwTWpneE1qYzJPREE0TlRrNkxUYzVOemM0TWpZAMU9EYzJNVGsyTXpjMk16RVBER0ZA3YVY5emRHOXllVjlwWkE4ZA01UVTNOREk0TVRJM05qZA3dPRFU1WHpFMU1UQXdNemM1TURVM05UTXlNREVQQkhScGJXVUdXZA0lOOWdFPQZDZD"
        }
    },
    "id": "123456789101112113"
};

function ImprimirObjeto(obj, retornarSaida)
{
    var saida = '';

    if($.isArray(obj) || typeof(obj) == 'object')
    {
        for(var i in obj)
            saida += i + ' => ' + ImprimirObjeto(obj[i], true) + '\n';
    }
    else
        saida += obj;

    if(retornarSaida && retornarSaida == true)
        return saida;
    else
    	console.log(saida);
}

ImprimirObjeto(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Credits to Rayiez.

If you want to print only one property you can use the map. Ex:

console.log(obj.posts.data.map(m => m.story));

Now, if the idea is to just read the object on the console you can use the stringify Ex:

JSON.stringify(obj);
  • Wow! Thank you. Actually the intention is to print the list directly into my own HTML. I used the console only as an example for testing.

Browser other questions tagged

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