【问题标题】:google places API parsing to javascript谷歌将 API 解析为 javascript
【发布时间】:2016-12-06 10:50:53
【问题描述】:

我有一个 google API URL,其中包含我希望在我的页面上输出的 json。

包含 id 和 key 的 URL

https://maps.googleapis.com/maps/api/place/details/json?placeid=122&key=123-mg

现在我想在div id myoutput中打印出以下数据

 "reviews" : [
         {
            "aspects" : [
               {
                  "rating" : 3,
                  "type" : "overall"
               }
            ],
            "author_name" : "Justine OBRIEN",
            "author_url" : "https://www.google.com/maps/contrib/104177669626132953795/reviews",
            "language" : "en",
            "profile_photo_url" : "//lh6.googleusercontent.com/-s6AzNe5Qcco/AAAAAAAAAAI/AAAAAAAAFTE/NvVzCuI-jMI/photo.jpg",
            "rating" : 5,
            "relative_time_description" : "2 months ago",
            "text" : "Fabulous location. Wonderful warm welcoming reception. Excellent unique living Google wall entry. Sensational helpful kind people. Easy fast efficient help online plus with appointment on site. Super company always progressive plus innovative products and services for all businesses. Go Google Australia. Shine on! ",
            "time" : 1474966415
         },

到目前为止我已经尝试过什么

<div id="myoutput"></div>

            <script>
                (function() {
                    var myobject = "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyCUc3pmryE8WMd7niIrHIN7cq_6ZUyI-mg";
                    $.getJSON( myobject, {

                        format: "json"
                    })
                            .done(function( data ) {
                                $.each( data.items, function( i, item ) {
                                    $( "<p>" ).attr( "author_name", item.media.m ).appendTo( "#myoutpu" );
                                    if ( i === 3 ) {
                                        return false;
                                    }
                                });
                            });
                })();
            </script>

<script>
                (function() {
                    var myobject = "https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJN1t_tDeuEmsRUsoyG83frY4&key=AIzaSyCUc3pmryE8WMd7niIrHIN7cq_6ZUyI-mg";
                    $.getJSON( myobject, {
                        format: "json"
                    })

                    print myobject.dumps([s['formatted_address'] for s in result['results']], indent=2)
                    myobject["address_components"].long_name

                })();
            </script>

我怎么看不到将author_name 打印出来?

【问题讨论】:

  • 从代码示例中删除您的密钥,然后进行更改。
  • 它不是我的钥匙,但我会这样做

标签: jquery json rest google-api


【解决方案1】:

在这种情况下,您的方法似乎被 CORS 阻止了,因此您可以像这样使用位置库:

<div id="map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_KEY&libraries=places&callback=initMap"></script>
<script>
function initMap() {
  var service = new google.maps.places.PlacesService(map);
  service.getDetails({
    placeId: 'ChIJN1t_tDeuEmsRUsoyG83frY4'
  }, function(place, status) {
    if (status === google.maps.places.PlacesServiceStatus.OK) {
      for(var i=0; i <= place.reviews.length; i++) {
        console.log(place.reviews[i]);
      }
    }
  });
}
</script>

map div 仅用于服务运行。循环是您一一显示所有评论对象的地方。在那里你可以对结果做你需要的事情。 此外,您可以从链接中删除&amp;callback=initMap,并在需要时运行该函数。

【讨论】:

  • 嗯明白了,我试着让它工作,看看附加了什么。谢谢,如果我能正常工作,我会接受
  • 基本上是一样的,只是使用google maps API的正常功能。如果您遇到困难,我很乐意为您提供帮助。
  • 非常感谢,我会尽快回复您(20 分钟)
  • 这看起来很棒,我怎么能在 html 中提取诸如 author_name 之类的单个对象?
  • 当然,在循环中只是append(place.reviews[i].author_name);reviews 项目是一个数组,因此您需要 [i] 但个人评论是一个对象,因此请使用 .author_name.author_link 等选择您需要的元素。只需运行我的函数并检查您的检查员控制台。你应该在那里设置一个数组。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 2018-04-04
  • 2014-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多