提问人:mika_972 提问时间:11/2/2023 最后编辑:mika_972 更新时间:11/2/2023 访问量:24
在 Google 脚本中解析 JSON 信息,没有重复的第一个标记
parse json information in google script with no recurring first tag
问:
在谷歌表格中,我有一个脚本可以从 url 中检索 JSon
function getTeamData() {
var DATA_SPREADSHEET_ID = "xxxx";
var ss = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
var TeamDataSheet = ss.getSheetByName("rawTeam");
TeamDataSheet.clearContents();
var url = 'xxxx';
var response = UrlFetchApp.fetch(url);
var content = response.getContentText();
var data = JSON.parse(content);
//Logger.log(JSON.stringify(data));
我遇到的问题是JSon内容看起来像这样
{
"03wi4": {
"statsIncId": 5546,
"rotowireId": 3795,
"name": "Saunders, Wesley",
"fantraxId": "03wi4",
"team": "NY",
"position": "SG"
},
"059gh": {
"statsIncId": 6321,
"rotowireId": 4919,
"name": "Booth, Phil",
"fantraxId": "059gh",
"team": "(N/A)",
"position": "PG"
},
"02nfe": {
"statsIncId": 5020,
"rotowireId": 3308,
"sportRadarId": "c555e067-c4d5-43f6-99af-716b6005cbba",
"name": "Henson, John",
"fantraxId": "02nfe",
"team": "NY",
"position": "PF"
},
"033b7": {
"statsIncId": 5235,
"rotowireId": 3538,
"name": "Raduljica, Miroslav",
"fantraxId": "033b7",
"team": "(N/A)",
"position": "C"
},
...
}
我希望能够为每个条目返回团队,但我没有通用的第一个标签名称...... 你能帮忙吗,结果应该是这样的:
059gh (N/A)
02nfe NY
033b7 (N/A)```
答:
0赞
Tanaike
11/2/2023
#1
我相信你的目标如下。
- 您希望从显示的 JSON 对象中检索值。
[["03wi4","NY"],["059gh","(N/A)"],["02nfe","NY"],["033b7","(N/A)"]]
- 您显示的 JSON 对象是 .
data
var data = JSON.parse(content);
在这种情况下,以下修改怎么样?
修改后的脚本:
function getTeamData() {
var DATA_SPREADSHEET_ID = "xxxx";
var ss = SpreadsheetApp.openById(DATA_SPREADSHEET_ID);
var TeamDataSheet = ss.getSheetByName("rawTeam");
TeamDataSheet.clearContents();
var url = 'xxxx';
var response = UrlFetchApp.fetch(url);
var content = response.getContentText();
var data = JSON.parse(content);
// I added the below script.
var res = Object.entries(data).map(([k, { team }]) => [k, team]);
TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
当您考虑之前的问题时,请按如下方式修改上述脚本。
从
TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
自
TeamDataSheet.getRange(1, 1, res.length, res[0].length).setNumberFormat("@").setValues(res);
或
从
var res = Object.entries(data).map(([k, { team }]) => [k, team]); TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
自
var res = Object.entries(data).map(([k, { team }]) => [`'${k}`, team]); TeamDataSheet.getRange(1, 1, res.length, res[0].length).setValues(res);
注意:
- 在此修改中,它假定 的值是显示的 JSON 对象。请注意这一点。
data
引用:
0赞
TheMaster
11/2/2023
#2
使用 Object.keys 和 Array.map 进行迭代:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const json = {
"03wi4": {
"statsIncId": 5546,
"rotowireId": 3795,
"name": "Saunders, Wesley",
"fantraxId": "03wi4",
"team": "NY",
"position": "SG"
},
"059gh": {
"statsIncId": 6321,
"rotowireId": 4919,
"name": "Booth, Phil",
"fantraxId": "059gh",
"team": "(N/A)",
"position": "PG"
},
"02nfe": {
"statsIncId": 5020,
"rotowireId": 3308,
"sportRadarId": "c555e067-c4d5-43f6-99af-716b6005cbba",
"name": "Henson, John",
"fantraxId": "02nfe",
"team": "NY",
"position": "PF"
},
"033b7": {
"statsIncId": 5235,
"rotowireId": 3538,
"name": "Raduljica, Miroslav",
"fantraxId": "033b7",
"team": "(N/A)",
"position": "C"
},
}
const values = Object.keys(json).map(k => [/*prepend ' for maintaining text format in sheets*/"'"+k, json[k].team])
console.table(values)
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
评论