尝试使用jQuery显示JSON文本数据 [英] Trying to use jQuery to display JSON text data

查看:164
本文介绍了尝试使用jQuery显示JSON文本数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道的很少(没有)的JavaScript,或者很多关于使用的API。不过,我想显示在我的网站的一些酒店点评,在qype.com API提供。不过我挣扎与能够进行管理。

I know very little (none) JavaScript, or much about using API's. However I would like to display some hotel reviews on my webiste made available over the qype.com API. However I'm struggling with being able to manage this.

这是code我到目前为止有:

This is the code I have so far:

$(document).ready( function() {
  $.getJSON( "http://api.yelp.com/business_review_search?term=hilton%20metropole&location=B26%203QJ&ywsid=APIKEY Removed",
    function(data) {
      $.each( data.businesses, function(i,businesses) {
        content = '<p>' + businesses.reviews.text_excerpt + '</p>';
        content = '<p>' + businesses.reviews.date + '</p>';
        $(content).appendTo("#review");
      } );
    }
  );
} );

我在体内被称为检讨的div,我想显示的文字。

I have a div in the body called review where I want to display the text.

任何意见极大好评。

JSON可以在<一个找到href=\"http://api.yelp.com/business%5Freview%5Fsearch?term=hilton%20metropole&location=B26%203QJ&ywsid=lOoGGbkYpVmTvxHlWGT2Lw\" rel=\"nofollow\">http://api.yelp.com/business%5Freview%5Fsearch?term=hilton%20metropole&location=B26%203QJ&ywsid=lOoGGbkYpVmTvxHlWGT2Lw

另外,我在同一页上的多个商家,我将如何使用在同一页上此多次,但产量在不同地点的数据?

Also, I have multiple businesses on the same page, how would I make use of this multiple times on the same page, but output the data in different locations?

推荐答案

更新:啊,我现在看到你的错误。 businesses.reviews 是一个数组(每个业务可以有更多的多篇评论),所以你必须遍历每个企业,每个企业的评论。

Update: Ah, I see your error now. businesses.reviews is an array (each business can have more than one review) so you have to loop over each business and each business' reviews.

我不得不改变一些事情让它在我的测试床运行,但你可以在这里看到运行此code的样本:的 http://bit.ly/4mTxPp

喊叫目前支持 JSONP 调用,所以你可以改变你的code到:

yelp currently support JSONP calls so you can change your code to:

<script src="http://code.jquery.com/jquery-latest.js"></script>

<script>
function showData(data) {
    $.each(data.businesses, function(i,business){
        // extra loop
        $.each(business.reviews, function(i,review){ 
            var content = '<p>' + review.text_excerpt + '</p>';
            content += '<p>' +review.date + '</p>';
            $(content).appendTo('#review');
        });
    });      
}


$(document).ready(function(){
    // note the use of the "callback" parameter
    writeScriptTag( "http://api.yelp.com/business_review_search?"+
    "term=hilton%20metropole"+
    "&location=B26%203QJ"+
    "&ywsid=lOoGGbkYpVmTvxHlWGT2Lw"+
    "&callback=showData"); // <- callback
});

function writeScriptTag(path) {
    var fileref = document.createElement('script');
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", path);

    document.body.appendChild(fileref);
}
</script>

这篇关于尝试使用jQuery显示JSON文本数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆