在 Google 資料通訊協定中使用 JSON

警告:本頁面說明 Google 的舊版 API (即 Google Data API);該 API 只與 Google Data API 目錄中列出的 API 相關,其中許多 API 已由新版 API 取代。如需特定新的 API 的相關資訊,請參閱新 API 的說明文件。如要瞭解如何使用新版 API 授權要求,請參閱 Google 帳戶驗證與授權

本文涵蓋 JSON 在 Google 資料通訊協定中使用的 JSON。如要進一步瞭解這項通訊協定,請參閱開發人員指南

您可以使用任何程式設計語言的 JSON,但本文件中的範例是以 JavaScript 編寫而成。

關於 JSON 資訊提供和 XML 資訊提供

Google Data 服務運用以下規則轉換 XML 資訊提供,藉此建立 JSON 格式的資訊提供:

Basic

  • 動態饋給會以 JSON 物件表示;每個巢狀元素或屬性都以物件的名稱/值屬性表示。
  • 屬性會轉換為字串屬性。
  • 子項元素會轉換為物件屬性。
  • 可能出現多次的元素會轉換為陣列屬性。
  • 標記的文字值會轉換為 $t 屬性。

命名空間

  • 如果元素具有命名空間別名,則別名和元素會使用「$」串連。例如,ns:element 會變成 ns$element

XML

  • XML 版本和編碼屬性會分別轉換為根元素的版本和編碼屬性。

以下為相同動態饋給的 XML 與 JSON 版本範例。這些分頁會以分頁顯示,方便您進行比較。

XML

<?xml version="1.0" encoding="UTF-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom"
    xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/"
    xmlns:gd="http://schemas.google.com/g/2005"
    xmlns:gCal="http://schemas.google.com/gCal/2005">
  <id>...</id>
  <updated>2006-11-12T21:25:30.000Z</updated>
  <title type="text">Google Developer Events</title>
  <subtitle type="text">The calendar contains information about upcoming
    developer conferences at which Google will be speaking, along with
    other developer-related events.</subtitle>
  <link rel="http://schemas.google.com/g/2005#feed"
    type="application/atom+xml" href="..." />
  <link rel="self" type="application/atom+xml" href="..." />
  <author>
    <name>Google Developer Calendar</name>
    <email>developer-calendar@google.com</email>
  </author>
  <generator version="1.0"
    uri="http://www.google.com/calendar">Google Calendar</generator>
  <openSearch:startIndex>1</openSearch:startIndex>
  <openSearch:itemsPerPage>25</openSearch:itemsPerPage>
  <gCal:timezone value="America/Los_Angeles" />
    
  <entry>
    <id>...</id>
    <published>2006-11-12T21:25:30.000Z</published>
    <updated>2006-11-12T21:25:30.000Z</updated>
    <category scheme="..." term="..." />
    <title type="text">WebmasterWorld PubCon 2006:
      Google Developer Tools in General</title>
    <content type="text">Google is sponsoring at 
      <a href="http://www.pubcon.com/">WebmasterWorld PubCon 2006</a>. Come and
      visit us at the booth or join us for an evening demo reception where we
      will be talking "5 ways to enhance your website with Google Code". 
      After all, it is Vegas, baby! See you soon.</content>
    <link rel="alternate" type="text/html" href="..." title="alternate" />
    <link rel="self" type="application/atom+xml" href="..." />
    <author>
      <name>Google Developer Calendar</name>
      <email>developer-calendar@google.com</email>
    </author>
    <gCal:sendEventNotifications value="true" />
    <gd:comments>
       <gd:feedLink href="..." />
    </gd:comments>
    <gd:transparency value="..." />
    <gd:eventStatus value="..." />
    <gd:where valueString="3150 Paradise Road, Las Vegas, NV 89109" />
    <gd:when startTime="2006-11-15" endTime="2006-11-17">
      <gd:reminder minutes="10" />
    </gd:when>
  </entry>
  ...
</feed>
    

JSON

{
"version": "1.0", "encoding": "UTF-8", "feed": { "xmlns": "http://www.w3.org/2005/Atom", "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/", "xmlns$gd": "http://schemas.google.com/g/2005", "xmlns$gCal": "http://schemas.google.com/gCal/2005", "id": {"$t": "..."}, "updated": {"$t": "2006-11-12T21:25:30.000Z"}, "title": { "type": "text", "$t": "Google Developer Events" }, "subtitle": { "type": "text", "$t": "The calendar contains information about upcoming developer conferences at which Google will be speaking, along with other developer-related events." }, "link": [{ "rel": "...", "type": "application/atom+xml", "href": "..." },{ "rel": "self", "type": "application/atom+xml", "href": "..." }], "author": [{ "name": {"$t": "Google Developer Calendar"}, "email": {"$t": "developer-calendar@google.com"} }], "generator":{ "version": "1.0", "uri": "http://www.google.com/calendar", "$t": "Google Calendar" }, "openSearch$startIndex": {"$t": "1"}, "openSearch$itemsPerPage": {"$t": "25"}, "gCal$timezone": {"value": "America/Los_Angeles"}, "entry": [{ "id": {"$t": "..."}, "published": {"$t": "2006-11-12T21:25:30.000Z"}, "updated": {"$t": "2006-11-12T21:25:30.000Z"}, "category": [{ "scheme": "...", "term": "..." }], "title":{ "type": "text", "$t": "WebmasterWorld PubCon 2006: Google Developer Tools in General" }, "content": { "type": "text", "$t": "Google is sponsoring at <a href="http://www.pubcon.com/">WebmasterWorld PubCon 2006</a>. \nCome and visit us at the booth or join us for an evening demo reception where we will be talking "5 ways to enhance your website with Google Code".\nAfter all,\nit is Vegas, baby! See you soon." }, "link": [{ "rel": "alternate", "type": "text/html", "href": "...", "title": "alternate" },{ "rel": "self", "type": "application/atom+xml", "href": "..." }], "author": [{ "name": {"$t": "Google Developer Calendar"}, "email": {"$t": "developer-calendar@google.com"} }], "gd$transparency": {"value": "http://schemas.google.com/g/2005#event.opaque"}, "gd$eventStatus": {"value": "http://schemas.google.com/g/2005#event.confirmed"}, "gd$comments": {"gd$feedLink": {"href": "..."}}, "gCal$sendEventNotifications": {"value": "true"}, "gd$when": [{ "startTime": "2006-11-15", "endTime": "2006-11-17", "gd$reminder": [{"minutes": "10"}] }], "gd$where": [{"valueString": "3150 Paradise Road,Las Vegas,NV 89109"}]}, }] } }

要求和使用 JSON 資訊提供

Atom 是 Google 資料的預設格式。如果您未在要求中指定 alt 參數,您就會收到 Atom 資訊提供。

注意:Google Data 用戶端程式庫目前不支援 JSON。

JSON 輸出

如要要求 JSON 格式的回應,請使用 alt=json 參數。

例如,若要要求使用 JSON 格式的 Google 開發人員日曆資訊提供,請傳送以下查詢:

http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json

JSON 指令碼內輸出結果

如要要求在 JSON 指令碼中納入 JSON 回應,請使用 alt=json-in-script 參數並新增 callback=functionName 參數來新增回呼函式。

http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=myFunction

使用回呼函式可讓您解決在一般用戶端 JavaScript 中,可能會遇到的一些跨網域安全性問題。一般來說,瀏覽器可能會因為安全風險和可能的跨網域攻擊,導致各網域無法載入檔案。

JSON-in-script 格式可讓您在用戶端的指令碼標記中載入回應,以克服這些限制。也就是說,您可以在網頁上的 JavaScript 程式碼中其他位置使用回應。網頁載入時,系統會載入 JSON 輸出結果,因此不需要任何其他技巧。

下方的簡單工作應用程式可展現這項技巧:

<h3>Upcoming Google Developer Events</h3>

<div id="agenda"></div>

<script>
  function listEvents(root) {
    var feed = root.feed;
    var entries = feed.entry || [];
    var html = ['<ul>'];

    for (var i = 0; i < entries.length; ++i) {
      var entry = entries[i];
      var title = (entry.title.type == 'html') ? entry.title.$t : escape(entry.title.$t);
      var start = (entry['gd$when']) ? entry['gd$when'][0].startTime : "";	

      html.push('<li>', start, ' ', title, '</li>');
    }

    html.push('</ul>');
    document.getElementById("agenda").innerHTML = html.join("");
  }
</script>

<script src="http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json-in-script&callback=listEvents">
</script>

範例

如需如何將 JSON 與 Google Data API 搭配使用的範例,請參閱範例頁面。

其他資源

建議您參考幾種外部資源,進一步瞭解 JSON,以及如何在網路應用程式中使用 JSON。

返回頁首