AlphaVantage API 的键中有空格和句点。API 没有正式的文档,尽管您可以在他们的演示网址中看到它
在我的 Typescript 应用程序中,我为此创建了数据结构(我很高兴任何人都可以复制和使用这些数据结构 - 也许在找到我的问题的解决方案之后):
export class MetaData {
'1. Information': string
'2. Symbol': string
'3. Last Refreshed': string
'4. Output Size': string
'5. Time Zone': string
constructor(one, two, three, four, five) {
this['1. Information'] = one
this['2. Symbol'] = two
this['3. Last Refreshed'] = three
this['4. Output Size'] = four
this['5. Time Zone'] = five
}
}
export interface TimeSeries {
[date: string]: {
'1. open': string;
'2. high': string;
'3. low': string;
'4. close': string;
'5. volume': string;
}
}
export interface AlphaVantage {
'Meta Data': MetaData;
'Time Series (Daily)'?: TimeSeries;
'Time Series (Weekly)'?: TimeSeries;
}
我使用 NPM 调用 APIalphavantage
并将其隐式转换为我的AlphaVantage
:
const av: AlphaVantage = await alpha.data.weekly(options.stocks, 'compact', 'json')
然后(可能经过一些按摩等)我将它保存在 MongoDB 集合中:
const doc = await this.model.findByIdAndUpdate(proxyModel._id, proxyModel)
(ProxyModel 是一个 DTO,用于定义数据库键,例如日期、股票代码等......其中一个字段是 AlphaVantage 数据)。
这必须对数据进行序列化,并且会出现以下错误:
key 1. Information must not contain '.'
有没有简单的方法来处理这个。我的选择是创建没有空格的等效对象:
export interface TimeSeries {
[date: string]: {
'1_open': string;
'2_high': string;
'3_low': string;
'4_close': string;
'5_volume': string;
}
}
然后投射到这个。在这种情况下,提供映射...
我可以看到我自己创建了一个实现。然而,在我开始之前,我想听听如何最好地处理这个数据结构的任何想法。