View categories of each post via JSON JS

Asked

Viewed 75 times

0

I’m using the blogger, and I need to "generate" the categories of each post.

function posts(json) {
 for (var i = 0; i < 11; i++) {
    var post = json.feed.entry[i];
    var postLabels = [];
  }
  for (var b = 0; b < post.category.length; b++) {
     postLabels.push(post.category[b].term);
   }
    document.write('<span>'+postLabels.join(', ')+'</span>');
  }
}

It works, but shows only the first category of each post. I need you to display all, as I do?

  "published": {
            "$t": "2014-09-06T22:43:00.001-07:00"
        }
        ,
        "updated": {
            "$t": "2018-04-15T13:24:56.217-07:00"
        }
        ,
        "category":[ {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Car"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Gallery"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "People"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Technology"
        }
        ,
        {
            "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste"
        }
        ],
        "title": {
            "type": "text", "$t": "Photodune Vintage Car"
 }
  • 1

    You have already tried console.log(json); to verify that all information is coming in?

  • Yes. There’s something missing in the code, I believe it’s not difficult, I’m really beginner.

  • 1

    Edit the question and put the return of the.log console to make it easy to answer.

1 answer

0


It is only displaying one category, because within the first loop for you declare the variable postLabels being an array, soon after in the second loop for, you define it again and define its value as a string, the correct is to add the value in postLabels.

You must change the line:

var postLabels = post.category[b].term;

for:

postLabels.push(post.category[b].term);

And change as well:

document.write('<span>'+postLabels+'</span>');

for:

document.write('<span>'+postLabels.join(', ')+'</span>');

Working example:

let json = {
  "feed": {
    "entry": [
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Car" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Gallery" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "People" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Technology" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste" }
        ],
        "title": {"type": "text", "$t": "Teste A" }
      },
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Car" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste" }
        ],
        "title": {"type": "text", "$t": "Teste B" }
      },
      {
        "published": { "$t": "2014-09-06T22:43:00.001-07:00" },
        "category": [
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste A" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste B" },
          { "scheme": "http://www.blogger.com/atom/ns#", "term": "Teste C" }
        ],
        "title": {"type": "text", "$t": "Teste C" }
      }
    ]
  }
};
function posts(json) {
  for (var i = 0; i < json.feed.entry.length; i++) {
    var post = json.feed.entry[i];
    var postTitle = post.title.$t;
    var postLabels = [];

    for (var b = 0; b < post.category.length; b++) {
      postLabels.push(post.category[b].term);
    }

    document.write('<article class="item">'); 
    document.write('<h2>'+postTitle+'</h2>');
    document.write('<span>'+postLabels.join(', ')+'</span>');
    document.write('</article>');
  }
}
posts(json)

The problem of repeating tags.

Note your code the lines I commented:

for (var k = 0; k < post.link.length; k++) {
  if (post.link[k].rel == 'alternate') {var postUrl = post.link[k].href;}
  if (post.link[k].rel == 'enclosure') {
    // post miniatura ↓
    if(post.link[k].type == 'poster imagem'){var postImage = post.link[k].href;}
    if(post.link[k].type == 'miniatura video'){var postImage = post.link[k].href;}
    if(post.link[k].href == 'http://subtitulo.com'){
      var enclosureLinkSubtitulo = post.link[k].type;
    }
    if(post.link[k].type == 'tempo real'){
      var enclosureLinkSubtitulo = "<span class='postSubTitulo' style='color:#FFF;font-weight:600;background:#cd0000;padding: 4px 5px 3px;margin-bottom:10px;display:inline-block'>TEMPO REAL</span>"
      }     
    // types icons ↓
    if(post.link[k].type == 'icone video'){var enclosureIcon = "<i class='fa fa-play'></i>";}
  }
  for (var u = 0; u < post.author.length; u++) {
    var postAuthor = post.author[u].name.$t;
  }

  // O PROBLEMA ESTA AQUI, ESTE FOR TEM QUE ESTAR FORA
  // DO FOR ATUAL.
  for (var b = 0; b < post.category.length; b++) {
      postLabels.push(post.category[b].term);
  }
}
  • It worked in the middle. He even displayed all the tags, however he keeps repeating them. For example: Tag1, Tag2, Tag1, Tag2, Tag1, Tag2, Tag1, Tag2...

  • 1

    It would be interesting if you edit your question and put a snippet of JSON.

  • I put the full code.

  • I’m sorry if I don’t understand, but this helps?

  • Okay. I put it, but it happens like I said before, it displays all the tags, but keeps repeating them over and over again.

  • 1

    Look at the example I put in the answer! It’s working normally. The reason I’m repeating this code you put in is because it’s inside another for (var k = 0; k < post.link.length; k++)

  • Right. Solved^^

  • 1

    Blz, glad I could help.

Show 3 more comments

Browser other questions tagged

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