前言:我是JS的新手,并且已经尝试使用谷歌搜索我的解决方案几个小时。
我正在使用Google表格(在脚本编辑器中)对公共端点进行API调用。这是我目前的代码:
function getMaterials() {
var myUrl = "https://api.guildwars2.com/v2/account/materials?access_token=[my_access_token]";
var jsonData = UrlFetchApp.fetch(myUrl);
var jsonString = jsonData.getContentText();
return jsonString;
}
以下是返回内容的片段:
[
{
"id": 12134,
"category": 5,
"count": 14
},
{
"id": 12238,
"category": 5,
"count": 0
},
{
"id": 12147,
"category": 5,
"count": 6
},
{
"id": 12142,
"category": 5,
"count": 12
},
{
"id": 12135,
"category": 5,
"count": 0
},
{
"id": 12246,
"category": 5,
"count": 1
}
]
以下是我正在与之交互的特定API的文档:
https://wiki.guildwars2.com/wiki/API:2/account/materials
没有真正的方法来过滤掉我的电话,或者我非常乐意使用它。
我希望查询“id”值,但只返回同一查询中的“count”值。我已尝试过各种网站的多种方式,但无法到达任何地方。帮助
答案 0 :(得分:1)
我最终找到了其他一些能够满足我需要的JS代码。
它允许我通过将数据格式化为列的函数来提供API。我可以将这些数据放在其他工作表中,然后将其输入我的主页。
答案 1 :(得分:0)
我找不到该端点的API文档,但我想您可以将数据转换为使用id作为键的对象。根据您的使用情况,最好只查询API以获取特定密钥(即,如果您只需要少量数据),如果可能的话,这会减少接收的数据量和内存使用量。但是如果没有这个,那应该会有所帮助!
var data = getMaterials();
// Map each object by ID
function getMaterialIdMap(data){
var materials = {};
for(var i = 0; i < data.length; i++ ){
var material = data[i];
if(material.hasOwnProperty('id')){ // Ensure we only get objects with ids
materials[material.id] = material;
}
}
return materials
}
// Map just the count to id
function getIdToCountMap(data){
var materials = {};
for(var i = 0; i < data.length; i++ ){
var material = data[i];
if(material.hasOwnProperty('id') && material.hasOwnProperty('count')){ // Ensure we only get objects with counts and ids
materials[material.id] = material.count;
}
}
return materials
}
// For the full object by id
var materialMap = getMaterialIdMap(data);
// For just the counts
var materialCountMap = getIdToCountMap(data);
// For use in the sheet =countFromId(12134) //14
function countFromId (id){
return materialCountMap[id];
}
function getMaterials(){
// Just a dummy data function for live snippet replace me with your version that performs the actual request and returns data as an array of objects.
return [
{
"id": 12134,
"category": 5,
"count": 14
},
{
"id": 12238,
"category": 5,
"count": 0
},
{
"id": 12147,
"category": 5,
"count": 6
},
{
"id": 12142,
"category": 5,
"count": 12
},
{
"id": 12135,
"category": 5,
"count": 0
},
{
"id": 12246,
"category": 5,
"count": 1
}
]
}var data = getMaterials();
// Map each object by ID
function getMaterialIdMap(data){
var materials = {};
for(var i = 0; i < data.length; i++ ){
var material = data[i];
if(material.hasOwnProperty('id')){ // Ensure we only get objects with ids
materials[material.id] = material;
}
}
return materials
}
// Map just the count to id
function getIdToCountMap(data){
var materials = {};
for(var i = 0; i < data.length; i++ ){
var material = data[i];
if(material.hasOwnProperty('id') && material.hasOwnProperty('count')){ // Ensure we only get objects with counts and ids
materials[material.id] = material.count;
}
}
return materials
}
var materialMap = getMaterialIdMap(data);
// For just the counts
var materialCountMap = getIdToCountMap(data);
// For the full object by id
function countFromId (id){
return materialCountMap[id];
}
function getMaterials(){
// Just a dummy data function for live snippet
return [
{
"id": 12134,
"category": 5,
"count": 14
},
{
"id": 12238,
"category": 5,
"count": 0
},
{
"id": 12147,
"category": 5,
"count": 6
},
{
"id": 12142,
"category": 5,
"count": 12
},
{
"id": 12135,
"category": 5,
"count": 0
},
{
"id": 12246,
"category": 5,
"count": 1
}
]
}