JSON POST导入

时间:2019-06-19 04:05:35

标签: javascript json post

我正在尝试将数据从JSON POST API导入Google表格。我已经能够记录完整的JSON响应,但是现在无法将其仅过滤到我需要的属性,因为在尝试记录DIV1时,我不断收到错误消息,指出该属性为“未定义”。我已经尝试过对它进行其他参数测试,例如DrawDate和DrawType,但始终收到相同的消息。

function main() {

// Make a POST request with a JSON payload.
var data = {
  'CompanyId': 'GoldenCasket',
  'MaxDrawCount': 1,
  'OptionalProductFilter':['OzLotto']};

var options = {
  'method' : 'post',
  'contentType': 'application/json',
  // Convert the JavaScript object to a JSON string.
  'payload' : JSON.stringify(data)};

  var response = UrlFetchApp.fetch('https://data.api.thelott.com/sales/vmax/web/data/lotto/opendraws', options);

  Logger.log('output: '+ response);          

  var json = JSON.parse(response)
  var DIV1 = json["Div1Amount"];

  Logger.log(DIV1) 
}

我收到了对该请求的答复,但我只想收到Div1Amount。

output: 
{
   "Draws":[
      {
         "ProductId":"OzLotto",
         "DrawNumber":1323,
         "DrawDisplayName":"Oz Lotto $80,000,000",
         "DrawDate":"2019-06-25T00:00:00",
         "DrawLogoUrl":"http://media.tatts.com/TattsServices/Lotto/Products/OzLotto_v1.png",
         "DrawType":"Jackpot",
         "Div1Amount":80000000.0000,
         "IsDiv1Estimated":false,
         "IsDiv1Unknown":false,
         "DrawCloseDateTimeUTC":"2019-06-25T09:35:00",
         "DrawEndSellDateTimeUTC":"2019-06-25T09:30:00",
         "DrawCountDownTimerSeconds":538150.0
      }
   ],
   "ErrorInfo":null,
   "Success":true
}

1 个答案:

答案 0 :(得分:0)

在您的回复中,Div1Amount属于Draws列表。因此,为此,您必须获取列表并遍历列表,然后var DIV1 = listObj["Div1Amount"];才能起作用。示例代码:

//after `var json = JSON.parse(response)` line, change your code with this:

var drawList=json ["Draws"];
//now iterate the list:
for(var i=0;i<drawList.length;i++)
{
  var DIV1=drawList[i]["Div1Amount"];
  console.log(DIV1);
}