【问题标题】:Is there way to get the fields as a JSON object instead of string from Google Spreadsheet?有没有办法将字段作为 JSON 对象而不是来自 Google 电子表格的字符串?
【发布时间】:2017-05-30 11:12:55
【问题描述】:

通过使用像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 的表示法的对象的字符串表示。后者使用JSON.parse 转换为前者。
  • @zaq 我附上了一个数据示例
  • @zaq 你可以看到它是带有: 的字符串。有没有办法可以查询它们,比如它的 sql 是否只获取需要的那些作为 json 对象?而不是字符串然后必须再次解析?

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


【解决方案1】:

没有“JSON 对象”这样的东西。有对象,在 JSON 中有表示这些对象的字符串。你得到一个字符串,并使用obj = JSON.parse(str) 将其解析为一个对象。单元格的内容隐藏在该对象的几层深处,可以使用

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

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


此外,您可以使用带有回调的 json-in-script:将 basic?alt=json 替换为

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

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

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

【讨论】:

    【解决方案2】:

    尝试“values?alt=json”而不是“basic?alt=json”
    通过使用“基本”,您将获得字符串格式的电子表格数据,并通过“值”获得对象格式的电子表格数据

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-12-22
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多