带有来自链接的属性的 JSON 附加响应

Posted

技术标签:

【中文标题】带有来自链接的属性的 JSON 附加响应【英文标题】:JSON append response with attribute from link 【发布时间】:2020-05-07 06:20:30 【问题描述】:

我在 Zapier 有 API 端点,我从工资系统调用所有已批准的休假请求。作为数据。

    const options = 
  url: 'https://api.somepayroll.com/api/v1/leaves/requests',
  method: 'GET',
  headers: 
    'Accept': 'application/json',
    'Authorization': `Bearer $bundle.authData.access_token`,
    'X-CLIENT-SECRET': bundle.authData.client_secret,
    'X-CLIENT-ID': bundle.authData.client_id,
    'X-STATUS': bundle.inputData.status
  ,
  params: 

  


return z.request(options)
  .then((response) => 
    response.throwForStatus();
    const results = z.JSON.parse(response.content);

    // You can do any parsing you need for results here before returning them

    return results.content;
  );

这让我得到以下回应:

[
  
    "employeeId": "1",
    "requestId": 5487,
    "hours": 8,
    "leaveFromDate": "19-Feb-2020",
    "leaveToDate": "19-Feb-2020",
    "reason": "Birthday Party",
    "status": "Pending",
    "payElement": "Annual Leave",
    "leaveBalanceType": 
      "leaveType": "Annual",
      "name": "Annual Leave",
      "unit": "hours",
      "organisationSpecific": false
    ,
    "payElementId": 50,
    "daysConsumed": 0,
    "daysCurrent": 0,
    "daysRemaining": 0,
    "quantityConsumed": 0,
    "quantityCurrent": 0,
    "quantityRemaining": 8,
    "additionalApprovalAcquired": false,
    "leaveInDays": false,
    "links": [
      
        "rel": "self",
        "href": "https://api.somepayroll.com/api/v1/leaves/requests/5487"
      ,
      
        "rel": "employee",
        "href": "https://api.somepayroll.com/api/v1/employees/1"
      ,
      
        "rel": "payelement",
        "href": "https://api.somepayroll.com/api/v1/payelements/AL"
      
    ],
    "id": "5487"
  ,
  
    "employeeId": "1",
    "requestId": 5551,
    "hours": 8,
    "leaveFromDate": "23-Jan-2020",
    "leaveToDate": "23-Jan-2020",
    "reason": "Camping",
    "status": "Pending",
    "payElement": "Annual Leave",
    "leaveBalanceType": 
      "leaveType": "Annual",
      "name": "Annual Leave",
      "unit": "hours",
      "organisationSpecific": false
    ,
    "payElementId": 50,
    "daysConsumed": 0,
    "daysCurrent": 0,
    "daysRemaining": 0,
    "quantityConsumed": 0,
    "quantityCurrent": 0,
    "quantityRemaining": 8,
    "additionalApprovalAcquired": false,
    "leaveInDays": false,
    "links": [
      
        "rel": "self",
        "href": "https://api.somepayroll.com/api/v1/leaves/requests/5551"
      ,
      
        "rel": "employee",
        "href": "https://api.somepayroll.com/api/v1/employees/1"
      ,
      
        "rel": "payelement",
        "href": "https://api.somepayroll.com/api/v1/payelements/AL"
      
    ],
    "id": "5551"
  
]

我想做的也是从回复https://api.somepayroll.com/api/v1/employees/1 中的链接中获取员工的姓名,因为它只给了我身份证号码。是否可以像这样交叉链接数据并匹配employeeID 并让名为firstName 的数据成为此结果的一部分?

【问题讨论】:

【参考方案1】:

它应该由后端处理,但是您可以从响应中调用链接并获取额外数据,

【讨论】:

谢谢,你知道我该怎么做吗? 将 xhr 获取请求发送到链接变量中的“api.somepayroll.com/api/v1/employees/1”,看看响应是什么

以上是关于带有来自链接的属性的 JSON 附加响应的主要内容,如果未能解决你的问题,请参考以下文章

如何使用循环将带有来自 googlesearch 的链接的列表附加到另一个列表?

当 Ajax 响应来自使用 select2 的 laravel 控制器时,如何检查 JQuery 附加选项中的条件?

Tableview 行没有在 swift 中显示带有附加值的顺序

如何附加来自 JSON 的数据

使用来自 JSON 的数据创建和附加多个元素

如何将 Ajax Json 响应附加到 html?