简体   繁体   English

Google表格:API返回的json标记未定义

[英]Google Sheets: API returning undefined for json tag

I've been trying to use an online API (albion-online-data.com) for a while in sheets, creating a little function to try to grab a specific identifier, sell_price_min, but the function keeps returning undefined. 我一直在尝试在表格中使用在线API(albion-online-data.com)一段时间,创建了一个小函数来尝试获取特定的标识符sell_price_min,但该函数始终返回未定义状态。

I've been looking around for ages but haven't been able to find out what's wrong. 我已经寻找了很多年了,但一直没能找出问题所在。 Sorry, I'm new to APIs and google sheets in general. 抱歉,我是API和Google表格的新手。 I used Logger.log, and it shows the correct contents after Json.parse, but when trying to use return w.sell_price_min; 我使用了Logger.log,它在Json.parse之后显示了正确的内容,但是在尝试使用return w.sell_price_min时; it always returns undefined. 它总是返回undefined。

Here's the code: 这是代码:

/**
 * Retrieve the current price for a given city.
 *
 */
function CURRENTPRICE(name, location, quality) {
    name = encodeURI(name);
    location = encodeURI(location);
    quality = encodeURI(quality);
    var response = UrlFetchApp.fetch("http://www.albion-online-data.com/api/v2/stats/Prices/" + name + "?locations=" + location + "&qualities=" + quality);
    var w = JSON.parse(response.getContentText());
    return w.sell_price_min;
}

and the API is: https://www.albion-online-data.com/api/v2/stats/prices/T4_BAG,T5_BAG?locations=Caerleon,Bridgewatch&qualities=2 并且API是: https//www.albion-online-data.com/api/v2/stats/prices/T4_BAG,T5_BAG?locations = Caerleon,Bridgewatch&quality = 2

which returns the following: (reformatted here with indentation and line breaks for readability) 它返回以下内容:( 为了便于阅读,此处使用缩进和换行符对其进行了重新格式化)

[
  {
    "item_id": "T4_BAG",
    "city": "Bridgewatch",
    "quality": 2,
    "sell_price_min": 4000,
    "sell_price_min_date": "2019-09-02T22:20:00",
    "sell_price_max": 4444,
    "sell_price_max_date": "2019-09-02T22:20:00",
    "buy_price_min": 0,
    "buy_price_min_date": "0001-01-01T00:00:00",
    "buy_price_max": 0,
    "buy_price_max_date": "0001-01-01T00:00:00"
  },
  {
    "item_id": "T4_BAG",
    "city": "Caerleon",
    "quality": 2,
    "sell_price_min": 5571,
    "sell_price_min_date": "2019-09-03T11:05:00",
    "sell_price_max": 5571,
    "sell_price_max_date": "2019-09-03T11:05:00",
    "buy_price_min": 2375,
    "buy_price_min_date": "2019-09-03T08:41:00",
    "buy_price_max": 4020,
    "buy_price_max_date": "2019-09-03T08:41:00"
  },
  {
    "item_id": "T5_BAG",
    "city": "Bridgewatch",
    "quality": 0,
    "sell_price_min": 20000,
    "sell_price_min_date": "2019-09-01T14:00:00",
    "sell_price_max": 22100,
    "sell_price_max_date": "2019-09-01T14:00:00",
    "buy_price_min": 0,
    "buy_price_min_date": "0001-01-01T00:00:00",
    "buy_price_max": 0,
    "buy_price_max_date": "0001-01-01T00:00:00"
  },
  {
    "item_id": "T5_BAG",
    "city": "Caerleon",
    "quality": 2,
    "sell_price_min": 23897,
    "sell_price_min_date": "2019-09-03T11:03:00",
    "sell_price_max": 26376,
    "sell_price_max_date": "2019-09-03T11:03:00",
    "buy_price_min": 15000,
    "buy_price_min_date": "2019-09-02T22:21:00",
    "buy_price_max": 22550,
    "buy_price_max_date": "2019-09-02T22:21:00"
  }
]

and the results should be: 4000? 结果应该是:4000?

Sorry for any hassle... I've been stumped for literally hours .-. 对不起,有什么麻烦...我已经被困了几个小时.-。

You are accessing a JSON array. 您正在访问JSON数组。 So it should be 所以应该

for(var i = 0; i < w.length; i++) {
    var obj = w[i];

    console.log(obj.sell_price_min);
} 

If you want to get the first value of the array put 如果要获取数组的第一个值放

return w[0].sell_price_min

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM