从Google Map解析JSON [英] Parsing JSON from Google Map

查看:104
本文介绍了从Google Map解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示这个json的地方详情:
我有解析它的问题,我想得到网站,名称和评级。
这是一个JSON链接: https://maps.googleapis.com/maps/api/place/details/json?reference=CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc&sensor=true&key=AIzaSyDSKYz8pCRLHglMPGo1ca6E-geDUQwFNQ8

I want to display place details from this json: I have problem with parse it, i want to get "website", "name" and "rating". This is a link to json: https://maps.googleapis.com/maps/api/place/details/json?reference=CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc&sensor=true&key=AIzaSyDSKYz8pCRLHglMPGo1ca6E-geDUQwFNQ8.

{

"html_attributions": [
    "Źródło wpisów: <a href=\"http://www.yellowpages.com.au/\">Yellow Pages</a>"
],
"result": {
    "address_components": [
        {
            "long_name": "10",
            "short_name": "10",
            "types": [
                "street_number"
            ]
        },
        {
            "long_name": "Darling Drive",
            "short_name": "Darling Drive",
            "types": [
                "route"
            ]
        },
        {
            "long_name": "Darling Harbour, Sydney",
            "short_name": "Darling Harbour, Sydney",
            "types": [
                "locality",
                "political"
            ]
        },
        {
            "long_name": "NSW",
            "short_name": "NSW",
            "types": [
                "administrative_area_level_1",
                "political"
            ]
        },
        {
            "long_name": "AU",
            "short_name": "AU",
            "types": [
                "country",
                "political"
            ]
        },
        {
            "long_name": "2000",
            "short_name": "2000",
            "types": [
                "postal_code"
            ]
        }
    ],
    "formatted_address": "Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney NSW, Australia",
    "formatted_phone_number": "(02) 9211 8900",
    "geometry": {
        "location": {
            "lat": -33.871983,
            "lng": 151.199086
        }
    },
    "icon": "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
    "id": "677679492a58049a7eae079e0890897eb953d79b",
    "international_phone_number": "+61 2 9211 8900",
    "name": "Zaaffran Restaurant - BBQ and GRILL, Darling Harbour",
    "rating": 3.9,
    "reference": "CpQBjAAAAARBNvpI6EgL3AuRxjwEMz9Va2gFaByrv-0GdqcoZ2QnyfmJiwEX1MLv2uCofWPqxkHPWQNwskrRD58LgD7egsuGooWPDmd2v1Xmt1YTLH0NF0yhXPfSiK62n6m2elI8u6h6FUW6eKJdQVGKLeRfLHl-Ezw_ML1kL4srZ80xuFZNWVmUyHWN2H5BDhhpGI4NwxIQAr5S5g1x1v4WvxS-zq2YBxoU0pVhJe6lLnkKDmm8fgYObLakvL8",
    "types": [
        "restaurant",
        "food",
        "establishment"
    ],
    "url": "http://maps.google.com/maps/place?cid=14075582311174685259",
    "vicinity": "Harbourside Centre 10 Darling Drive, Darling Harbour, Sydney",
    "website": "http://www.zaaffran.com/"
},
"status": "OK"

}

我试图使用此功能:

$(document).ready(function() {

   $.getJSON('GOOGLE URL HERE', function(data) {
     $('#dictionary').empty();
      $.each(data, function(entryIndex, entry) {
         var html = '<div class="entry">';
         html += '<h3 class="term">' + entry['name'] + '</h3>';
         html += '<div class="part">' + entry['website'] + '</div>';
         html += '<div class="definition">';
         html += entry['rating'];
         html += '</div>';
         html += '</div>';
         $('#dictionary').append(html);
      });
   });
   return false;

});

它没有'为我工作。
请帮助我,非常感谢。
Michal

It doesn't work for me. Help me please, thanks a lot. Michal

推荐答案

替换:

 $.each(data, function(entryIndex, entry) {
     var html = '<div class="entry">';
     html += '<h3 class="term">' + entry['name'] + '</h3>';
     html += '<div class="part">' + entry['website'] + '</div>';
     html += '<div class="definition">';
     html += entry['rating'];
     html += '</div>';
     html += '</div>';
     $('#dictionary').append(html);
  });

with:

     var html = '<div class="entry">';
     html += '<h3 class="term">' + data.result['name'] + '</h3>';
     html += '<div class="part">' + data.result['website'] + '</div>';
     html += '<div class="definition">';
     html += entry['rating'];
     html += '</div>';
     html += '</div>';
     $('#dictionary').append(html);

不需要每个。使用 data.result.name 直接访问数据,因为 data 是单个对象以及结果

The each is not needed. Access the data directly with data.result.name, since data is a single object as well as result

这篇关于从Google Map解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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