Javascript $.getJSON结果转换为html

Javascript $.getJSON结果转换为html,javascript,jquery,html,getjson,handlebars.js,Javascript,Jquery,Html,Getjson,Handlebars.js,我在这里读了一些例子,但是我仍然有点困惑。 我从一个api获取一个提要,这个api的格式如下 { total:123, id: "1234", results: [ { id: "1234", name: "bobs market", phone: "123-456-7890", othervalue: "33333333", onemore: "get this"

我在这里读了一些例子,但是我仍然有点困惑。 我从一个api获取一个提要,这个api的格式如下

{
total:123,
id: "1234",
results: [
           {
           id: "1234",
           name: "bobs market",
           phone: "123-456-7890",
           othervalue: "33333333",
           onemore: "get this"
           },
           {
           id: "1235",
           name: "jans market",
           phone: "123-456-7899",
           othervalue: "33333353",
           onemore: "get this one"
           }
         ]
}
<div id="business">
      <h4><a href=#>Business Name</a></h4>
      <p id="phone">555-555-5555</p>
      <p id="adr">
      <span id="street-address">12 Main Street</span>
      <span id="locality">Sacramento</span>
      <span id="region">CA</span>
</div>
我试图使用$.getJSON特性循环遍历每个记录,并在一个id为#bizresults的div中显示每个业务,因此生成的html如下所示

{
total:123,
id: "1234",
results: [
           {
           id: "1234",
           name: "bobs market",
           phone: "123-456-7890",
           othervalue: "33333333",
           onemore: "get this"
           },
           {
           id: "1235",
           name: "jans market",
           phone: "123-456-7899",
           othervalue: "33333353",
           onemore: "get this one"
           }
         ]
}
<div id="business">
      <h4><a href=#>Business Name</a></h4>
      <p id="phone">555-555-5555</p>
      <p id="adr">
      <span id="street-address">12 Main Street</span>
      <span id="locality">Sacramento</span>
      <span id="region">CA</span>
</div>

555-555-5555

大街12号 萨克拉门托 加利福尼亚州

我似乎在脚本中得到了结果,我可以在Chrome开发控制台中看到它们,但是我似乎无法将结果输出到我的div

接受建议,我看了@handlebar.js**我还是不太明白--


{{{#每个jsonResult}

{{phone}

{{地址} {{城市} {{state}}

{{/每个}} var source=$(“#biz_模板”).html(); var template=handlebar.compile(源代码); var数据=“{”totalCount“:61,“impId:”17“,”jsonResult:“{”impId:”17“,”listingId:”94523“,”pageUrl:“/page/LA/new orleans/ccs coffee house/17-94523.html“,”姓名“:”CC's coffee house“,”电话“:”(504)586-0278“,”地址“:”波伊德拉斯街650号“,”城市“,”新奥尔良“,”州“:”LA“,”纬度“:”29.949339“,”经度“,”90.070017“,”,“,”impId:”17“,”listingId“:”417428”,“pageUrl”:“/page/LA/Metarie/ccs社区咖啡馆/17-417428.html”,“姓名”:“CC社区咖啡馆”,“电话”:“(504)831-1449”,“地址”:“701 Metarie路”,“城市”:“Metarie”,“州”:“LA”,“纬度”:“29.98763”,“经度”:“-90.130528”},{“impId”:“17”,“列表ID”:“228835”,“pageUrl”:“/page/LA/new orleans/ccs community coffee house/17-228835.html”,“姓名”:“Cc社区咖啡馆”,“电话”:(504)566-1863”,“地址”:“228圣查尔斯大道”,“城市”:“新奥尔良”,“州”:“洛杉矶”,“纬度”:“29.951952”,“经度”:“-90.069885”}”; $(“#biz”).html(模板(数据));
有什么想法吗


我在HTML中确实有id为biz的div,这里的任务是在getJSON成功函数中建立标记。你可以通过压缩字符串或使用模板系统(如Handlebar、JsRender、下划线等)来实现。如果你想进行双向绑定,请查看Angular或Knockout。

我刚刚用
$.getJSON
和handlebar,这只是它的一部分,基本上就是你所需要的。如果你愿意,我可以发布整个插件

不要忘记将文件或url引用到把手

<script src="http://cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js" type="text/javascript"></script>

var container = $('#tweets'),
    template: $('#tweets-template').html()

$.getJSON(this.url, function(data) {
                    var tweets = $.map(data.results, function(tweet) {
                        return {
                            author: tweet.from_user,
                            tweet: tweet.text,
                            thumb: tweet.profile_image_url,
                            url: 'http://twitter.com/' + tweet.from_user + '/status/' + tweet.id_str
                        };
                    });

                    var templateHandlebars = Handlebars.compile(template);
                    container.append(templateHandlebars(tweets));
});


//this is the html and the template
<ul id="tweets">
        <script id="tweets-template" type="text/x-handlebars-template">
            {{#each this}}
                <li>
                    <img src={{thumb}} alt={{author}} />
                    <p><a href={{url}}>{{tweet}}</a></p>
                </li>
            {{/each}}
        </script>
</ul>

var container=$(“#tweets”),
模板:$(“#推文模板”).html()
$.getJSON(this.url,函数(数据){
var tweets=$.map(data.results,function)(tweet){
返回{
作者:tweet.from_用户,
tweet:tweet.text,
拇指:tweet.profile\u image\u url,
网址:'http://twitter.com/'+tweet.from_user+'/status/'+tweet.id_str
};
});
var templatehandlebar=handlebar.compile(模板);
container.append(templatehandlebar(tweets));
});
//这是html和模板
    {{{#每个这个}
  • {{/每个}}

请发布JavaScript代码。这就是这里的重点…如果你想做一次,可以这样做->使用adeneo给出的示例。如果你想多次做这种事情,胡子将非常有助于使用模板引擎(从下划线到把手)好的-看着这个车把看起来很有趣,但是我从来没有用过它-我会到处看看-但是那里的任何指导都会帮助我前进-