有没有办法将字段作为JSON对象而不是Google Spreadsheet中的字符串?

时间:2017-01-15 14:48:17

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

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

的网址

我得到的字段数据在entry下作为数组,并且在每个数据中,内容都是以字符串形式获取,可能会有问题需要解析。例如,当字段的内容变为content: abc is here并且冒号是分隔符时。很多情况都可以。但是,当我更改包含不仅仅是abcab content: c的值的字段值时,我尝试过很容易混淆,好吧,没有编写很多解析代码。< / p>

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

示例:

{
  "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"
}

2 个答案:

答案 0 :(得分: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&amp;callback=yourFunction

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

两者?alt = json和?alt = json-in-script是遗留API,但显然它们仍然可以工作。

答案 1 :(得分:1)

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