【问题标题】:Trouble parsing JSON Google Place data解析 JSON Google Place 数据时遇到问题
【发布时间】:2019-06-17 00:21:19
【问题描述】:

我正在尝试从 Google 获得对某个地点的评分。我正在通过 placeID 执行 JSON 请求,请求字段名称和评级。当我在浏览器中查看 JSON url 时,我看到以下数据:

{
   "html_attributions" : [],
   "result" : {
      "name" : "Coffeetots Play Café",
      "rating" : 4.7
   },
   "status" : "OK"
}

我在尝试将“评级”字段保存为 javascript/jQuery 中的变量或将其显示在页面上时遇到问题。我的代码在下面(删除了我的 API 密钥),我假设我不需要“每个”循环,因为只有一个地方,但我还没有找到任何其他方法。

<div id="results"></div>

<script type="text/javascript">
$.getJSON("https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJgRUw-0_dekgRWCHhui-rPBg&fields=name,rating&key=XXX", function(json){
  $.each(json.results, function(i, place){
    myRating = place.rating;
    $("#results").append('<p>'+myRating+'</p>');
  });
});
</script>

任何帮助将不胜感激!

【问题讨论】:

  • 假设您的示例是正确的,那么您只需要$("#results").append('&lt;p&gt;'+ json.result.rating+'&lt;/p&gt;'); - 即没有每个
  • 谢谢,但这似乎也不起作用。
  • 有错误吗?为了清楚起见,您确实用该行替换了 each() 循环,对吗?
  • 尝试了使用和不使用 each 循环。这是一个 ;codepen 的链接codepen.io/fraig/pen/wNBYPm?editors=1000
  • 您的笔不工作,由于 CORS 问题和 jQuery 放置,调用不工作

标签: jquery json google-maps-api-3


【解决方案1】:

根据您在问题中提供的 JSON,您应该使用 json.result 而不是 json.results

{
   "html_attributions" : [],
   "result" : {
      "name" : "Coffeetots Play Café",
      "rating" : 4.7
   },
   "status" : "OK"
}

【讨论】:

    【解决方案2】:

    查看示例结果,您会得到一个看起来不像您的示例的数组。

    这可以使用此处找到的 google 示例:https://developers.google.com/places/web-service/search

    var json = {
       "html_attributions" : [],
       "next_page_token" : "CpQCAgEAAFxg8o-eU7_uKn7Yqjana-HQIx1hr5BrT4zBaEko29ANsXtp9mrqN0yrKWhf-y2PUpHRLQb1GT-mtxNcXou8TwkXhi1Jbk-ReY7oulyuvKSQrw1lgJElggGlo0d6indiH1U-tDwquw4tU_UXoQ_sj8OBo8XBUuWjuuFShqmLMP-0W59Vr6CaXdLrF8M3wFR4dUUhSf5UC4QCLaOMVP92lyh0OdtF_m_9Dt7lz-Wniod9zDrHeDsz_by570K3jL1VuDKTl_U1cJ0mzz_zDHGfOUf7VU1kVIs1WnM9SGvnm8YZURLTtMLMWx8-doGUE56Af_VfKjGDYW361OOIj9GmkyCFtaoCmTMIr5kgyeUSnB-IEhDlzujVrV6O9Mt7N4DagR6RGhT3g1viYLS4kO5YindU6dm3GIof1Q",
       "results" : [
          {
             "geometry" : {
                "location" : {
                   "lat" : -33.867217,
                   "lng" : 151.195939
                }
             },
             "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/cafe-71.png",
             "id" : "7eaf747a3f6dc078868cd65efc8d3bc62fff77d7",
             "name" : "Biaggio Cafe - Pyrmont",
             "opening_hours" : {
                "open_now" : true
             },
             "photos" : [
                {
                   "height" : 600,
                   "html_attributions" : [],
                   "photo_reference" : "CnRnAAAAmWmj0BqA0Jorm1_vjAvx1n6c7ZNBxyY-U9x99-oNyOxvMjDlo2npJzyIq7c3EK1YyoNXdMFDcRPzwLJtBzXAwCUFDGo_RtLRGBPJTA2CoerPdC5yvT2SjfDwH4bFf5MrznB0_YWa4Y2Qo7ABtAxgeBIQv46sGBwVNJQDI36Wd3PFYBoUTlVXa0wn-zRITjGp0zLEBh8oIBE",
                   "width" : 900
                }
             ],
             "place_id" : "ChIJIfBAsjeuEmsRdgu9Pl1Ps48",
             "scope" : "GOOGLE",
             "price_level" : 1,
             "rating" : 3.4,
             "reference" : "CoQBeAAAAGu0wNJjuZ40DMrRe3mpn7fhlfIK1mf_ce5hgkhfM79u-lqy0G2mnmcueTq2JGWu9wsgS1ctZDHTY_pcqFFJyQNV2P-kdhoRIeYRHeDfbWtIwr3RgFf2zzFBXHgNjSq-PSzX_OU6OT2_3dzdhhpV-bPezomtrarW4DsGl9uh773yEhDJT6R3V8Fyvl_xeE761DTCGhT1jJ3floFI5_c-bHgGLVwH1g-cbQ",
             "types" : [ "cafe", "bar", "restaurant", "food", "establishment" ],
             "vicinity" : "48 Pirrama Rd, Pyrmont"
          },
          {
             "geometry" : {
                "location" : {
                   "lat" : -33.866786,
                   "lng" : 151.195633
                }
             },
             "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
             "id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
             "name" : "Doltone House",
             "photos" : [
                {
                   "height" : 1260,
                   "html_attributions" : [ "From a Google User" ],
                   "photo_reference" : "CnRwAAAAeM-aLqAm573T44qnNe8bGMkr_BOh1MOVQaA9CCggqtTwuGD1rjsviMyueX_G4-mabgH41Vpr8L27sh-VfZZ8TNCI4FyBiGk0P4fPxjb5Z1LrBZScYzM1glRxR-YjeHd2PWVEqB9cKZB349QqQveJLRIQYKq2PNlOM0toJocR5b_oYRoUYIipdBjMfdUyJN4MZUmhCsTMQwg",
                   "width" : 1890
                }
             ],
             "place_id" : "ChIJ5xQ7szeuEmsRs6Kj7YFZE9k",
             "scope" : "GOOGLE",
             "reference" : "CnRvAAAA22k1PAGyDxAgHZk6ErHh_h_mLUK_8XNFLvixPJHXRbCzg-gw1ZxdqUwA_8EseDuEZKolBs82orIQH4m6-afDZV9VcpggokHD9x7HdMi9TnJDmGb9Bdh8f-Od4DK0fASNBL7Me3CsAWkUMWhlNQNYExIQ05W7VbxDTQe2Kh9TiL840hoUZfiO0q2HgDHSUyRdvTQx5Rs2SBU",
             "types" : [ "food", "establishment" ],
             "vicinity" : "48 Pirrama Rd, Pyrmont"
          }
       ],
       "status" : "OK"
    }
    
     $.each(json.results, function(i, place){
        myRating = place.rating || "not rated"
        $("#results").append('<p>'+i+':'+' '+place.name+' '+myRating+'</p>');
      });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="results"></div>

    如果您通过 ID 拨打电话,您会根据 https://developers.google.com/places/web-service/place-id 获得此信息

    var json =     {
          "html_attributions" : [],
          "results" : [
            {
              "geometry" : {
                "location" : {
                  "lat" : -33.870775,
                  "lng" : 151.199025
                }
              },
              "place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
              "name": "Somewhere",
              "rating": 4.5
            }
          ],
          "status" : "OK"
        }
    
    
     $.each(json.results, function(i, place){
        myRating = place.rating || "not rated"
        $("#results").append('<p>'+i+':'+' '+place.name+' '+myRating+'</p>');
      });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="results"></div>

    如果你真的拿回你展示的对象,那么你不能使用每个:

    var json =    {
       "html_attributions" : [],
       "result" : {
          "name" : "Coffeetots Play Café",
          "rating" : 4.7
       },
       "status" : "OK"
    }
    
    
     myRating = json.result.rating || "not rated";
     $("#results").append('<p>'+json.result.name+' '+myRating+'</p>');
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <div id="results"></div>

    【讨论】:

    • 谢谢,但这对我没有帮助。该文档用于地点搜索 - 我知道地点,我只想通过 ID 调用它,并返回地点名称和地点等级。
    • 所以请显示一个完整的结果——你称之为结果,它看起来是一个数组,但你显示的是一个对象。
    • 这是我不知道的地方,这就是我在这里的原因!调用 JSON 文件时,我返回的数据就是我放在 OP 中的数据。
    • 根据我的调查,您在结果中得到了一个数组"results" : [,但您有一个对象。看我的第二个例子
    • 根据您的对象查看我的第三个示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2012-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多