【发布时间】:2022-01-23 11:50:05
【问题描述】:
我已经设法使用 Google Apps 脚本下载了一个 JSON 文件。很快,我发现我的 JSON 比 Internet 上的示例(包括这里)要复杂一些。
var json = {"block1":[{"TRD_DD":"2021/12/22","MKTCAP":"2,208,300,470,743,887","FORN_HD_MKTCAP":"740,740,079,190,175","MKTCAP_RTO":"33.54","LIST_SHRS":"61,701,475,584","FORN_HD_SHRS":"11,191,374,601","LIST_SHRS_RTO":"18.14"},{"TRD_DD":"2021/12/21","MKTCAP":"2,200,896,109,564,335","FORN_HD_MKTCAP":"736,382,905,931,787","MKTCAP_RTO":"33.46","LIST_SHRS":"61,701,475,584","FORN_HD_SHRS":"11,198,948,564","LIST_SHRS_RTO":"18.15"},{"TRD_DD":"2021/12/20","MKTCAP":"2,191,456,183,748,063","FORN_HD_MKTCAP":"731,379,899,525,089","MKTCAP_RTO":"33.37","LIST_SHRS":"61,698,916,232","FORN_HD_SHRS":"11,215,916,966","LIST_SHRS_RTO":"18.18"}],"CURRENT_DATETIME":"2021.12.22 PM 04:24:06"}
“Block1”中有更多数组,但我缩短了长度。请考虑多行(我认为大约 500 行)
我想给出不同的列名,例如“AAA”、“BBB”、“CCC”、“DDD”、“EEE”、“FFF”、“GGG”,而不是“TRD_DD”、“MKTCAP”、 “FORN_HD_MKTCAP”、“MKTCAP_RTO”、“LIST_SHRS”、“FORN_HD_SHRS”、“LIST_SHRS_RTO”。 每行都应该插入同名的每个值(抱歉,StackOverflow 不允许我插入图片。这很难解释)。
如果可能的话,我想将所有数字更改为数字而不是字符串。
我是处理javascript的初学者,所以非常难!!! 有没有人可以帮助我?如果有的话,将不胜感激。
***附录
这是我的原始脚本
function foreign_daily(){
var url = 'http://data.krx.co.kr/comm/bldAttendant/getJsonData.cmd';
var formData = {
"bld": "dbms/MDC/STAT/standard/MDCSTAT03601",
"mktId": "STK",
"strtDd": "20211220",
"endDd": "20211222",
"share": "2",
"money": "3",
"csvxls_isNo": "false"};
var headers = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "ko-KR,ko;q=0.9,en-US;q=0.8,en;q=0.7",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Origin": "http://data.krx.co.kr",
"Referer": "http://data.krx.co.kr/contents/MDC/MDI/mdiLoader/index.cmd?menuId=MDC0201020503",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.118 Whale/2.11.126.23 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
};
Logger.log(JSON.stringify(formData));
var options = {
'method' : 'post',
'contentType': 'application/x-www-form-urlencoded; charset=UTF-8',
'headers': headers,
'payload': formData
};
var json = UrlFetchApp.fetch(url, options);
// Object for converting the keys.
var obj = {"TRD_DD": "AAA", "MKTCAP": "BBB", "FORN_HD_MKTCAP": "CCC", "MKTCAP_RTO": "DDD", "LIST_SHRS": "EEE", "FORN_HD_SHRS": "FFF", "LIST_SHRS_RTO": "GGG"};
// Converting keys.
json.block1 = json.block1.map(o => Object.fromEntries(Object.entries(o).map(([k, v]) => [obj[k], v.includes(",") ? v.split(",").map(e => Number(e)) : v])));
console.log(json)
// Put the values to Spreadsheet.
var header = ["AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"];
var values = json.block1.flatMap(o => {
var temp = header.map(h => o[h]);
var max = Math.max(...temp.map(r => Array.isArray(r) ? r.length : 1));
temp = temp.map(r => {
if (Array.isArray(r)) {
return r.length == max ? r : [...r, ...Array(max - r.length).fill("")];
}
return [r, ...Array(max - 1).fill("")]; // or return Array(max).fill(r);
})
return temp[0].map((_, c) => temp.map(r => r[c]));
});
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet126"); // Please set the sheet name.
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
现在它说
TypeError: Cannot read property 'map' of undefined
foreign_daily @ Foreign_daily.gs:42
再次感谢,田池!
【问题讨论】:
标签: json google-apps-script google-sheets