简体   繁体   English

如何使用Handlebars.js访问JSON对象?

[英]How do I access JSON objects using Handlebars.js?

I'm creating a basic web app using the Echo Nest API to get used to templating and fetching data from APIs. 我正在使用Echo Nest API创建一个基本的Web应用程序,以习惯于从API模板化和获取数据。 I was just wondering how I would access the 'artist_name' & 'title' objects inside 'song' from the JSON object and use Handlebars.js to insert this data inside my template? 我只是想知道如何从JSON对象访问“歌曲”中的“ artist_name”和“ title”对象,并使用Handlebars.js将此数据插入模板中? When I do the {{#each songs}} as I've written below, it doesn't work. 当我按照下面的说明操作{{#each songs}}时,它不起作用。

Thanks in advance! 提前致谢!

PS. PS。 Jeanie Tracy does not reflect my music taste. 珍妮·特雷西(Jeanie Tracy)没有反映我的音乐品味。 It's just the JSON object that came up when I was testing it! 测试时只是出现的JSON对象!

<main>
  <section>
    <input type="text" id="genre-name" placeholder="Enter the Genre Here." />
    <input type="number" id="bpm" placeholder="Enter the BPM here." id="bpm">
    <a href="#" id="fetch-albums">Fetch</a>
  </section>

  <section id="results">
  </section>

</main>

<template id="results-template">
  <article>
    <header>
      {{#each songs}}
      <h1>{{ this.artist_name }}</h1> {{/each}}
</template>

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="handlebars.js"></script>
<script type="text/javascript">
  $('#fetch-albums').on('click', function() {
    var genre = $('#genre-name').val();
    var bpm = $('#bpm').val();
    var source = $('#results-template').html();
    var template = Handlebars.compile(source);
    $.get('http://developer.echonest.com/api/v4/song/search?api_key=[hidden]&style=' + genre + '&min_danceability=0.65&min_tempo=' + bpm + '&results=5', function(data) {
      $('#results').append(template(data));
    });
  });
</script>

JSON OBJECT JSON对象

{
  "response": {
    "status": {
      "version": "4.2",
      "code": 0,
      "message": "Success"
    },
    "songs": [{
      "artist_id": "AR8COOH1187B990D7D",
      "id": "SOGMVZZ1393A2A9142",
      "artist_name": "Jeanie Tracy",
      "title": "I'm Gonna Get You"
    }, {
      "artist_id": "AR8COOH1187B990D7D",
      "id": "SOGMIVN14248BD9E88",
      "artist_name": "Jeanie Tracy",
      "title": "Feel Like Dancing [Joey Negro Dubbed Out]"
    }, {
      "artist_id": "AR8COOH1187B990D7D",
      "id": "SOIMUFZ1315CD4CDEC",
      "artist_name": "Jeanie Tracy",
      "title": "Do You Believe In Wonder (Stone & Nick Late Nite Diner Mix)"
    }, {
      "artist_id": "AR8COOH1187B990D7D",
      "id": "SOEQTUW1315CD4FAB2",
      "artist_name": "Jeanie Tracy",
      "title": "Intro"
    }, {
      "artist_id": "AR8COOH1187B990D7D",
      "id": "SOENYBA12A6D4F46C0",
      "artist_name": "Jeanie Tracy",
      "title": "Rosabel's Disco Diva Mix"
    }]
  }
}
  1. Your <article> and <header> don't have closing tags. 您的<article><header>没有结束标记。 While the HTML specifications may allow some tags to omit closing (like <td> , afaik), and while some browsers are forgiving enough, I don't know if the Handlebars compiler is the same. 尽管HTML规范可能允许某些标记省略关闭(例如<td> ,afaik),并且尽管某些浏览器已经足够宽容,但我不知道Handlebars编译器是否相同。

  2. Your songs is still under response . 您的songs仍在response I think it should be {{#each response.songs}} . 我认为应该是{{#each response.songs}} Also, I think you can go with {{ artist_name }} too. 另外,我认为您也可以使用{{ artist_name }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM