首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >有没有办法从Google电子表格中以JSON对象而不是字符串的形式获取字段?

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

Stack Overflow用户
提问于 2017-01-15 22:48:17
回答 2查看 623关注 0票数 0

通过使用像https://spreadsheets.google.com/feeds/list/mysheetid/od6/public/basic?alt=json这样的url

我将字段数据作为数组放在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"
}

EN

回答 2

Stack Overflow用户

发布于 2017-01-15 23:11:40

没有"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都是遗留的应用程序接口,但显然它们仍然有效。

票数 1
EN

Stack Overflow用户

发布于 2021-06-14 13:26:46

试着用"values?alt=json“代替"basic?alt=json”

通过使用'basic‘,您将获得字符串格式的电子表格数据,通过'values’,您将获得对象格式的电子表格数据

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41662425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档