将 JSON 数据导入 Google 表格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2658980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:27:50  来源:igfitidea点击:

Import JSON data into Google Sheets

jsongoogle-apps-scriptgoogle-sheets

提问by joejoeson

I am pulling data from a web service and it is formatted as JSON. I am writing a Google Apps Script for Google Sheets that will populate the data for me. My problem is, I can't seem to get it to parse out.

我正在从 Web 服务中提取数据,它的格式为 JSON。我正在为 Google Sheets 编写一个 Google Apps 脚本,它将为我填充数据。我的问题是,我似乎无法解析它。

Doing:

正在做:

var dataset = myJSONtext;
Browser.msgbox(dataset.item[0].key); 

errors out, saying:

错误,说:

item[0] is not defined.

项目[0] 未定义。

Is there some built in way I should be doing this?

我应该这样做吗?

采纳答案by Tim McNamara

Apps script is (pretty much) just Javascript; plain-old JSON.parseis your best option for parsing JSON into an object representation.

Apps 脚本(几乎)只是 Javascript;plain-oldJSON.parse是将 JSON 解析为对象表示的最佳选择。

You can also use JSON.stringifyto serialize an object into a string representation.

您还可以使用JSON.stringify将对象序列化为字符串表示形式。

回答by Anh-Kiet Ngo

JSON.parse

JSON.parse

For those who are seeing this in 2011+, as pointed outby Henrique Abreu at the Google support forum, Utilities.jsonParse is/will be deprecated. As you can see from the thread, there's a bug with this function that it does not work when your keys are numbers, ie "1234".

对于那些在 2011 年以后看到这个的人,正如Henrique Abreu 在 Google 支持论坛上指出的那样,Utilities.jsonParse 将被弃用。正如您从线程中看到的那样,此功能存在一个错误,即当您的键为数字(即“1234”)时它不起作用。

As suggested, you should be using JSON.stringify/parse.

按照建议,您应该使用 JSON.stringify/parse。

回答by garec

A 2013 update -- Check out the ImportJSON library at

2013 年更新——查看 ImportJSON 库,网址为

http://blog.fastfedora.com/projects/import-json

http://blog.fastfedora.com/projects/import-json

"ImportJSON imports data from public JSON APIs into Google Spreadsheets. It aims to operate similarly to how the native Google Spreadsheet functions ImportData and ImportXML work."

“ImportJSON 将数据从公共 JSON API 导入到 Google 电子表格中。它的目的是类似于原生 Google 电子表格函数 ImportData 和 ImportXML 的工作方式。”

Code available here and he has submitted it to the Script Gallery: https://raw.github.com/fastfedora/google-docs/master/scripts/ImportJSON/Code.gs

此处提供代码,他已将其提交到脚本库:https: //raw.github.com/fastfedora/google-docs/master/scripts/ImportJSON/Code.gs

Example usage: After putting the code in your Google spreadsheet's Script Editor, then paste this in cell A1 of the sheet:

用法示例:将代码放入 Google 电子表格的脚本编辑器后,然后将其粘贴到工作表的 A1 单元格中:

=ImportJSON("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json", "/feed/entry/title,/feed/entry/content",               "noInherit,noTruncate,rawHeaders")

回答by user3657546

Use this gist : https://gist.github.com/varun-raj/5350595a730a62ca1954

使用这个要点:https: //gist.github.com/varun-raj/5350595a730a62ca1954

Replace

代替

http://example.com/feeds?type=json

with your JSON url

使用您的 JSON 网址

Add your entities here

在此处添加您的实体

rows.push([data.id, data.name,data.email]);