2011-11-08 73 views
1

我想顯示這個JSON的地方詳細信息: 我有解析它的問題,我想獲得「網站」,「名稱」和「評級」。 這是json的鏈接:https://maps.googleapis.com/maps/api/place/details/json?reference=CpQBjAAAAHDHuimUQATR6gfoWNmZlk5dKUKq_n46BpSzPQCjk1m9glTKkiAHH_Gs4xGttdOSj35WJJDAV90dAPnNnZK2OaxMgogdeHKQhIedh6UduFrW53wtwXigUfpAzsCgIzYNI0UQtCj38cr_DE56RH4Wi9d2bWbbIuRyDX6tx2Fmk2EQzO_lVJ-oq4ZY5uI6I75RnxIQJ6smWUVVIHup9Jvc517DKhoUidfNPyQZZIgGiXS_SwGQ1wg0gtc&sensor=true&key=AIzaSyDSKYz8pCRLHglMPGo1ca6E-geDUQwFNQ8從谷歌地圖解析JSON

{ 

"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; 

});

它不適合我。 請幫助我,非常感謝。 米哈爾

回答

2

取代:

$.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); 
    }); 

有:

 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); 

是不需要的each。由於data是一個單一的對象以及result

+0

http://pastebin.com/vmTT8eSg - 這是我完整的網站代碼,它仍然無法正常工作。 – Michal

+0

它應該是data.result.name,而不是data.result ['name']。 – Jemaclus

+0

好的,謝謝我改變它:http://pastebin.com/X3GwcpAt。沒有錯誤,也許這是與谷歌API的問題?它仍然不起作用。 :( – Michal