在 Google 数据协议中使用 JSON

警告:本页面介绍的是 Google 的旧版 API,即 Google 数据 API;它仅与 Google 数据 API 目录中列出的许多 API 相关,其中许多 API 已替换为较新的 API。如需了解特定新 API,请参阅新 API 的文档。如需了解如何使用较新的 API 向请求授权,请参阅 Google 帐号身份验证和授权

本文介绍了在 Google 数据协议中使用的 JSON。如需详细了解该协议,请参阅开发者指南

您可以对任何编程语言使用 JSON,但本文档中的示例使用 JavaScript。

关于 JSON Feed 和 XML Feed

Google 数据服务通过使用以下规则转换 XML Feed 来创建 JSON 格式的 Feed:

基本版

  • Feed 表示为 JSON 对象;每个嵌套元素或属性表示为对象的名称/值属性。
  • 属性将转换为字符串属性。
  • 子元素会转换为对象属性。
  • 可能出现多次的元素会被转换为数组属性。
  • 标记的文本值会转换为 $t 属性。

命名空间

  • 如果某个元素有命名空间别名,则该别名和元素会使用“$”串联起来。例如,ns:element 会变为 ns$element

XML

  • XML 版本和编码属性会分别转换为根元素的版本和编码属性。

以下示例展示了同一 Feed 的 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 Feed

Atom 是 Google 数据的默认格式。如果您未在请求中指定 alt 参数,则会收到 Atom Feed。

注意:Google 数据客户端库目前不支持 JSON。

JSON 输出

要请求 JSON 格式的响应,请使用 alt=json 参数。

例如,要以 JSON 格式请求 Google 的开发者日历 Feed,请发送以下查询:

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>

示例

有关如何将 JSON 与 Google Data API 结合使用的实际示例,请参阅示例页面。

其他资源

您可能需要查看多个外部资源,详细了解 JSON 并在 Web 应用中使用 JSON。

返回页首