javascript - 有没有办法从 Google 电子表格中获取 JSON 对象而不是字符串形式的字段?

标签 javascript json url google-sheets google-sheets-api

使用类似 https://spreadsheets.google.com/feeds/list/mysheetid/od6/public/basic?alt=json

的网址

我得到的字段数据作为数组位于 entry 下,并且在每个字段数据中,内容均以纯字符串形式获取,解析可能会出现问题。例如,当字段的内容变为 content: abc is here 并且冒号是分隔符时。对于很多情况来说都是可以的。但是当我更改字段值时,我尝试过,该字段值不仅包含 abc 的值,还包含 ab content: c 的值,那么它很容易被混淆,嗯,没有 编写大量解析代码。

我想知道是否需要编写这种解析器才能将字段中的所有值作为实际对象获取,或者我只需要自己编写? (我知道怎么做,但如果 Google 电子表格已经提供了我不知道的功能,我不想重新发明轮子)。

示例:

{
  "encoding": "UTF-8",
  "feed": {
    "author": [
      {
        "email": {
          "$t": "adamchenwei@gmail.com"
        },
        "name": {
          "$t": "adamchenwei"
        }
      }
    ],
    "category": [
      {
        "scheme": "http://schemas.google.com/spreadsheets/2006",
        "term": "http://schemas.google.com/spreadsheets/2006#list"
      }
    ],
    "entry": [
      {
        "category": [
          {
            "scheme": "http://schemas.google.com/spreadsheets/2006",
            "term": "http://schemas.google.com/spreadsheets/2006#list"
          }
        ],
        "content": {
          "$t": "_cokwr: some value: afield:",
          "type": "text"
        },
        "id": {
          "$t": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cokwr"
        },
        "link": [
          {
            "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cokwr",
            "rel": "self",
            "type": "application/atom+xml"
          }
        ],
        "title": {
          "$t": "afield",
          "type": "text"
        },
        "updated": {
          "$t": "2017-01-15T15:05:08.549Z"
        }
      },
      {
        "category": [
          {
            "scheme": "http://schemas.google.com/spreadsheets/2006",
            "term": "http://schemas.google.com/spreadsheets/2006#list"
          }
        ],
        "content": {
          "$t": "_cokwr: antoerh value",
          "type": "text"
        },
        "id": {
          "$t": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cpzh4"
        },
        "link": [
          {
            "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cpzh4",
            "rel": "self",
            "type": "application/atom+xml"
          }
        ],
        "title": {
          "$t": "antoerh",
          "type": "text"
        },
        "updated": {
          "$t": "2017-01-15T15:05:08.549Z"
        }
      },
      {
        "category": [
          {
            "scheme": "http://schemas.google.com/spreadsheets/2006",
            "term": "http://schemas.google.com/spreadsheets/2006#list"
          }
        ],
        "content": {
          "$t": "_cokwr: 3rd something",
          "type": "text"
        },
        "id": {
          "$t": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cre1l"
        },
        "link": [
          {
            "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/cre1l",
            "rel": "self",
            "type": "application/atom+xml"
          }
        ],
        "title": {
          "$t": " 3rd",
          "type": "text"
        },
        "updated": {
          "$t": "2017-01-15T15:05:08.549Z"
        }
      },
      {
        "category": [
          {
            "scheme": "http://schemas.google.com/spreadsheets/2006",
            "term": "http://schemas.google.com/spreadsheets/2006#list"
          }
        ],
        "content": {
          "$t": "_cokwr: ,heyNewField:",
          "type": "text"
        },
        "id": {
          "$t": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/chk2m"
        },
        "link": [
          {
            "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic/chk2m",
            "rel": "self",
            "type": "application/atom+xml"
          }
        ],
        "title": {
          "$t": "heyNewField",
          "type": "text"
        },
        "updated": {
          "$t": "2017-01-15T15:05:08.549Z"
        }
      }
    ],
    "id": {
      "$t": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic"
    },
    "link": [
      {
        "href": "https://docs.google.com/spreadsheets/d/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/pubhtml",
        "rel": "alternate",
        "type": "application/atom+xml"
      },
      {
        "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic",
        "rel": "http://schemas.google.com/g/2005#feed",
        "type": "application/atom+xml"
      },
      {
        "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic",
        "rel": "http://schemas.google.com/g/2005#post",
        "type": "application/atom+xml"
      },
      {
        "href": "https://spreadsheets.google.com/feeds/list/1DvBoPjfiGNft4IcDwTKYNHYq_FLDlU5_4oI7AOhPzaM/od6/public/basic?alt=json",
        "rel": "self",
        "type": "application/atom+xml"
      }
    ],
    "openSearch$startIndex": {
      "$t": "1"
    },
    "openSearch$totalResults": {
      "$t": "4"
    },
    "title": {
      "$t": "Sheet1",
      "type": "text"
    },
    "updated": {
      "$t": "2017-01-15T15:05:08.549Z"
    },
    "xmlns": "http://www.w3.org/2005/Atom",
    "xmlns$gsx": "http://schemas.google.com/spreadsheets/2006/extended",
    "xmlns$openSearch": "http://a9.com/-/spec/opensearchrss/1.0/"
  },
  "version": "1.0"
}

最佳答案

不存在“JSON 对象”这样的东西。有对象,也有在 JSON 中表示这些对象的字符串。您获得一个字符串,并使用 obj = JSON.parse(str) 来将其解析为对象。单元格的内容被埋藏在该对象的几层深处,可以通过

访问
var content = obj.feed.entry[0].content.$t

(或用另一个索引代替 0)。不用担心包含冒号之类的内容,它们是用引号正确分隔的,并且会被正确解析。当然,您不应该编写自己的 JSON 解析器;有一个built in在 JavaScript 中。

<小时/>

此外,您可以将 json-in-script 与回调一起使用:将 basic?alt=json 替换为

basic?alt=json-in-script&amp;callback=yourFunction

位于请求 URL 的末尾。这里 yourFunction 是执行请求的 JS 文件中的函数名称。将使用已从该 JSON 字符串解析的对象作为参数来调用此函数。这种方法除了节省一个解析步骤之外,还避免了 CORS 问题。

?alt=json 和 ?alt=json-in-script 都是遗留 API,但显然它们仍然有效。

关于javascript - 有没有办法从 Google 电子表格中获取 JSON 对象而不是字符串形式的字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41662425/

相关文章:

javascript - 模块路由在 Express 中不起作用

php - 如何将一个 JavaScript 对象复制到另一个对象中?

javascript - 如何在 bootstrap modal 中将输入焦点设置为 kendo-ui 网格输入

javascript - 将 json 字符串转换为适合 esri.geometry.Multipoint 的字符串

java - 使用 Java-ee-7.0 for IBM 从 Jax-rs 1.1 迁移到 2.0 时出现的问题为 9

django - 将 Django admin 目录保留在 admin 的默认 url 下是否危险?

url - 谷歌短网址如何运作?

docker - SEO URL 在带有 Docker 的 OpenCart 2.3 中给出 404 错误

javascript - JQuery:转换为文本区域元素

javascript - 如何通过ajax更新js对象类型变量?