Google Data Protocol での JSON の使用

警告: このページは、Google の古い API である Google Data API を対象としています。Google Data API ディレクトリに記載されている API のみを対象としており、その多くは新しい API に置き換えられています。特定の新しい API については、その新しい API のドキュメントをご覧ください。新しい API を使用してリクエストを承認する方法については、Google アカウントの認証と承認をご覧ください。

この記事では、Google Data Protocol 内で使用される JSON について説明します。プロトコルの詳細については、デベロッパー ガイドをご覧ください。

任意のプログラミング言語で JSON を使用できますが、このドキュメントの例では JavaScript を使用しています。

JSON フィードと XML フィードについて

Google データサービスは、次のルールを使用して XML フィードを変換することにより、JSON 形式のフィードを作成します。

Basic

  • フィードは JSON オブジェクトとして表現され、ネストされた各要素または属性は、オブジェクトの名前/値プロパティとして表されます。
  • 属性は文字列プロパティに変換されます。
  • 子要素はオブジェクト プロパティに変換されます。
  • 複数回表示される要素は配列プロパティに変換されます。
  • タグのテキスト値は $t プロパティに変換されます。

Namespace

  • 要素に名前空間エイリアスがある場合、エイリアスと要素は「$」を使用して連結されます。たとえば、ns:elementns$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 パラメータを使用します。

たとえば、Google のデベロッパー カレンダー フィードを JSON 形式でリクエストするには、次のクエリを送信します。

http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=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>

サンプル

Google Data API で JSON を使用する方法の実例については、サンプルページをご覧ください。

参考情報

ウェブ アプリケーションで JSON と JSON を使用する場合の詳細については、いくつかの外部リソースをご覧ください。

トップへ戻る