首页 > 解决方案 > 如何更深入地了解 chainlink req.add("path") 请求的 JSON 正文?添加 2+ 路径

问题描述

我可以得到chainlink路径的结果req.add("path", "chainlink")

但是,我想返回 的价格值"chainlink", "USD"。输出的json有两条路径,如何到达第二条路径获取价格值?

  function requestLINKPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://api.coingecko.com/api/v3/simple/price?ids=chainlink&vs_currencies=usd");
    req.add("path", "chainlinkUSD");
    req.addInt("times", 100);
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }

这是 API 的 JSON 响应

{
  chainlink: {
    usd: 3.78
  }
}

标签: blockchainethereumsolidity

解决方案


您可以将复制适配器copyPath语法一起使用。

string[] memory copyPath = new string[](2);
copyPath[0] = "chainlink";
copyPath[1] = "USD";
req.addStringArray("copyPath", copyPath);

这是整个功能。

  function requestLINKPrice() 
    public
    onlyOwner
  {
    Chainlink.Request memory req = buildChainlinkRequest(JOB, address(this), this.fulfill.selector);
    req.add("get", "https://api.coingecko.com/api/v3/simple/price?ids=chainlink&vs_currencies=usd");
    string[] memory copyPath = new string[](2);
    copyPath[0] = "chainlink";
    copyPath[1] = "USD";
    req.addStringArray("copyPath", copyPath);
    req.addInt("times", 100);
    sendChainlinkRequestTo(ORACLE, req, ORACLE_PAYMENT);
  }

推荐阅读