- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经在下面设置了对 seatgeek 的 API 调用,但我在构建响应时遇到了问题。我想将“stats”属性中的四个标题以及其他几个标题写入我的电子表格。我不知道如何正确提取数据,我相信这是因为我不知道如何设置语法来建立标题。以下是 seatgeek 文档 ( or you can find here - about 1/3 down the page under Events ):
{
"stats": {
"listing_count": 161,
"average_price": 97,
"lowest_price": 62,
"highest_price": 296
},
为了完成调用,我设置了如下代码:
function myFunction() {
var url = 'A1';
var response = UrlFetchApp.fetch(url);
var json = response.getContentText();
var data = JSON.parse(json);
Logger.log(data);
}
我关注了一个 youtube 视频,所以它可以工作,但它的结构不正确。如果我检查记录器,这将返回我猜是 xml 格式(如下)?这是一串数据,如果我通读它,果然,我正在寻找的“统计数据”就埋在中间。
19-11-21 16:08:32:853 CST] {meta={per_page=1, took=3, total=1, page=1,
geolocation=null}, in_hand={}, events=[{venue={display_location=Denver,
CO, country=US, access_method={method=NONE, created_at=2015-11-
04T17:30:28Z, employee_only=false}, address=1000 Chopper Circle,
city=Denver, timezone=America/Denver,
url=https://seatgeek.com/venues/pepsi-center/tickets, capacity=21000,
extended_address=Denver, CO 80204, score=0.7527942,
num_upcoming_events=106, name_v2=Pepsi Center, metro_code=751,
popularity=0, name=Pepsi Center, links=[], location={lon=-105.008,
lat=39.7487}, id=187, state=CO, postal_code=80204, slug=pepsi-center,
has_upcoming_events=true}, access_method={method=NONE, created_at=2015-
11-04T17:30:28Z, employee_only=false}, conditional=false,
date_tbd=false, description=, created_at=2019-08-12T20:15:53,
title=Portland Trail Blazers at Denver Nuggets, type=nba, score=0.687,
short_title=Trail Blazers at Nuggets, visible_until_utc=2019-12-
13T07:30:00, stats={lowest_sg_base_price_good_deals=13,
visible_listing_count=718, lowest_price_good_deals=20,
average_price=120, lowest_price=20, highest_price=1780,
lowest_sg_base_price=13, listing_count=885, dq_bucket_counts=[86, 283,
162, 66, 72, 106, 72, 5], median_price=80}, popularity=0.758, links=[],
id=4960016, performers=[{image=https://seatgeek.com/images/performers-
landscape/denver-nuggets-ea3cb6/2103/huge.jpg, images= .
{huge=https://seatgeek.com/images/performers-landscape/denver-nuggets-
ea3cb6/2103/huge.jpg},
image_license=https://creativecommons.org/licenses/by/2.0/, type=nba,
colors={all=[#0E2240, #FEC524, #0E2240], iconic=#0E2240, primary= .
[#0E2240, #FEC524]}, divisions=[{display_type=Conference,
taxonomy_id=1030100, short_name=null, display_name=Western Conference,
slug=null, division_level=1}, {display_type=Division,
taxonomy_id=1030100, short_name=Northwest, display_name=Western -
Northwest, slug=western-northwest, division_level=2}],
url=https://seatgeek.com/denver-nuggets-tickets, score=0.7,
num_upcoming_events=69, stats={event_count=69}, taxonomies= .
[{parent_id=null, name=sports, document_source={source_type=ELASTIC,
generation_type=FULL}, id=1000000}, {parent_id=1000000,
name=basketball, document_source={source_type=ELASTIC,
generation_type=FULL}, id=1030000}, {parent_id=1030000, name=nba,
document_source={source_type=ELASTIC, generation_type=FULL},
id=1030100}], popularity=0,
image_attribution=https://www.flickr.com/photos/34247743@N07/
5342565327/in/photolist-9973xi-996Uxp-996Qwr-9971mx-pW93vv-9ddXGD-
9dcgRk-9dh49b-eXUzb7-eXUyqJ-eXUyJU-gAGfkV-9ddVN4-efsxLC-efmNi4-nLwpvg-
efmNqB-bGHTnP-btP6DQ-btP2NY-btP36U-bGHQX2-bGHSEK-btP3Wm-bGHRQc-bGHSa2-
9dfrVw-7qjEsp-6t2y82-7qoAD9-7qozqW-7qoyRj-7qoyg3-6QTVX7-jrx2ht-a8hFoL-
9dgYYm-rY1RES-s1iU4H-rJi58H-r4X6SX-s1FLi4-m1dYBZ-nMXgaJ-rZKUfA-rFyrmk-
rZTsUc-rZKTmb-9myz8B-rJ3c8q, name=Denver Nuggets, location= .
{lon=-105.008, lat=39.7487}, short_name=Nuggets, id=2103,
home_team=true, home_venue_id=187, slug=denver-nuggets, primary=true,
has_upcoming_events=true},
{image=https://seatgeek.com/images/performers-landscape/portland-trail-
blazers-d0de88/2106/huge.jpg, images= .
{huge=https://seatgeek.com/images/performers-landscape/portland-trail-
blazers-d0de88/2106/huge.jpg},
image_license=https://creativecommons.org/licenses/by-sa/3.0/deed.en,
type=nba, colors={all=[#191919, #D01631, #191919], iconic=#D01631,
primary=[#191919, #D01631]}, divisions=[{display_type=Conference,
taxonomy_id=1030100, short_name=null, display_name=Western Conference,
slug=null, division_level=1}, {display_type=Division,
taxonomy_id=1030100, short_name=Northwest, display_name=Western -
Northwest, slug=western-northwest, division_level=2}],
url=https://seatgeek.com/portland-trail-blazers-tickets,
away_team=true, score=0.71, num_upcoming_events=67, stats= .
{event_count=67}, taxonomies=[{parent_id=null, name=sports,
document_source={source_type=ELASTIC, generation_type=FULL},
id=1000000}, {parent_id=1000000, name=basketball, document_source= .
{source_type=ELASTIC, generation_type=FULL}, id=1030000},
{parent_id=1030000, name=nba, document_source={source_type=ELASTIC,
generation_type=FULL}, id=1030100}], popularity=0,
datetime_utc=2019-12-13T03:30:00, enddatetime_utc=null,
url=https://seatgeek.com/trail-blazers-at-nuggets-tickets/12-12-2019-
denver-colorado-pepsi-center/nba/4960016, event_promotion=null,
announce_date=2019-08-12T00:00:00, datetime_tbd=false, taxonomies= .
[{parent_id=null, name=sports, id=1000000}, {parent_id=1000000,
name=basketball, id=1030000}, {parent_id=1030000, name=nba,
id=1030100}], datetime_local=2019-12-12T20:30:00, time_tbd=false,
announcements={}, status=normal}]}
youtube 视频调整了他们的最后一行以指定他们想要从中提取数据的属性。如果我效仿并在最后一行添加“stats”(如下所示),我会收到“未定义”错误。
Logger.log(data.stats)
即使如此,我也不想只是“记录”通话,我希望它在我的电子表格中。如何使用正确的标题调用数据并将其设置为粘贴到我的工作表中?
最佳答案
请熟悉JSON .
您所说的“XML 对象”实际上是 JSON 对象的“字符串”版本。
你只是accessing properties当它们(属性)不存在时在您的对象上。
为了证明我的观点,这里有一个实验:
代码:
function myFunction() {
var j = {
"meta": [],
"in_hand": [],
"events": [
{"a":"1"},
{"a":"2"}
]
};
Logger.log(j); //Here is the "XML" version of the object, as you said.
Logger.log(j.doesnotexist); //This will say undefined, meaning this property does not exist.
}
输出:{meta=[], in_hand=[], events=[{a=1}, {a=2}]}
考虑到这一点,下面是解决您的问题的方法。 API 返回的对象采用以下格式:
{
meta,
in_hand,
events = [{
venue = {},
access_method = {},
conditional=false,
date_tbd=false,
// Other properties here, then, finally:
stats = {},
// Then more properties here.
}]
}
假设您的 data
变量包含 JSON.parse
的结果,您的 stats
变量应该是:
var stats = data.events[0].stats;
此外,如果您要返回多个事件,您可以有一个像这样的统计数据数组:
var stats = [];
for (var i = 0; i<data.events.length; i++) {
stats.push(data.events[i].stats);
}
希望我能够澄清并指导您找到解决方案。保重。
关于javascript - 如何格式化我的 api 调用以在 google 电子表格中发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58966544/
我正在从事的项目需要使用 toString 方法打印银行帐户余额。我不允许向当前程序添加任何方法,但我需要将 myBalance 变量格式化为 double 型,该变量保留两位小数而不是一位。在这个特
我喜欢 Visual Studio (2008) 格式化 C# 代码的方式;不幸的是,在编写 C++ 代码时,它的行为方式似乎有所不同。 比如我这样写代码的时候: class Test { publi
Scanner scan = new Scanner(System.in); System.out.println("Enter three positive integers seperated b
在 aspose(使用 C#)中用于格式化数字的正确样式属性是什么。我想做两件事: 1) 将五位数字格式化为邮政编码。(我不太确定使用哪个 Style 属性来获取自定义 excel 邮政编码格式) 2
我希望用户输入从 00 到 23 和从 00 到 59 的 Local.Time 的小时和分钟,我将其扫描为一个 int。它有效,但对于从 00 到 09 的值,int 忽略 0,然后将其放置为 0,
大家好, 请查看上面的图片,我有两张 table 。在下面代码的第一个表中,我得到了这种格式。 但我想像 Table2 那样格式化,每个合并单元格中的行数是动态的,而且不一样。 有没有办法像table
我在一个 laravel 网站工作,我只是想知道是否有办法为用户格式化 created_at 值,因为现在它类似于以下内容: 2017-09-20 13:41 但我更愿意将其格式化为易于阅读的格式,例
我正在尝试在 JTextPane 中进行一些基本的格式化。为此,我决定使用 html(HTMLDocument 和 HTMLEditorKit)。 这里是按钮的操作监听器代码,应使所选文本变为粗体 b
是否有规则或插件会导致 es-lint 错误或警告,如果范围内的声明没有像下面那样间隔,赋值运算符的对齐方式相同? var a = 'a'; var bb = 'b'; var ccc = 'd
我正在尝试重新格式化 LinkedHashMap 的输出以排除逗号和大括号。 这是我的看跌期权: token.put("[Server.Protocol]", url.getProtocol() +
我有一个程序,可以从文本文件中读取大量文本,然后根据文本内容随机化内容以显示为短篇故事。该程序可以运行,但最后一部分(我显示的 Material )非常笨重且效率不高,我想知道是否有人对如何更有效地获
我正在尝试将 VIM 作为我的 ruby/rails 编辑器。太胖了,我对它的功能印象深刻 并且我能够安装以下插件以提供更好的 IDE 体验 自动配对 Better-snipmate-snippe
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我有两个 jQuery 函数。我想先运行下面的第一个,然后在该函数完成后运行另一个。通常我会在代码的第一部分添加一个函数,但不确定如何使用这个 block 来做到这一点,或者它是否是正确的方法。 简而
在 Python 2.7 中,我有一个包含数字 1-25 字符串的列表(例如)。打印它们时,我希望列表打印 5 行,每行 5 个数字。 理想情况下: >>> print(myList) ['1', '
我有以下功能来打印借阅者已取出的书籍列表。 void searchBorrowerLoans(int a) { int i; for (i = 1
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 5 年前。 Improv
我正在尝试实现一个用作 ListView header 的 TextView。我想对其进行格式化,以便我基本上有一个以第一行为中心的标题,然后是以下几行的附加信息。如何格式化文本/字符串以创建这种格式
我尝试格式化数字字段 select to_char(12315.83453, 'FM999999999999D9999') 在这种情况下一切正常。结果是 12315.8345 但是如果值在0到1之间
我有一个带有 BigDecimal 字段的类。我把这个对象放到 JSP(Spring MVC)中。而且我需要显示十进制不带零的 BigDecimal 字段(20.00 就像 20),这就是为什么我的
我是一名优秀的程序员,十分优秀!