如何使用chainlink oracle读取json文件中的数组



我遇到了这个问题中提到的相同问题:如何使用链连接oracle读取JSON文件

但是,由于某种原因,我不能得到它的修复。我正试图从公海API读取(https://api.opensea.io/api/v1/events?asset_contract_address=0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D&event_type=successful&format=json&only_opensea=false&token_id=5407)

这是我的代码:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
/**
* THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
* PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
contract GetSalePrice is ChainlinkClient {
using Chainlink for Chainlink.Request;

//bytes32 public salePriceBytes32;
string  public salePrice;

address private oracle;
bytes32 private jobId;
uint256 private fee;

/**
* Network: Kovan
* Oracle: 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8 (Chainlink Devrel   
* Node)
* Job ID: 7401f318127148a894c00c292e486ffd
* Fee: 0.1 LINK
*/
constructor() {
setPublicChainlinkToken();
oracle = 0xc57B33452b4F7BB189bB5AfaE9cc4aBa1f7a4FD8;
jobId = "7401f318127148a894c00c292e486ffd";
fee = 0.1 * 10 ** 18; // (Varies by network and job)
}

/**
* Create a Chainlink request to retrieve API response, find the target
* data, then multiply by 1000000000000000000 (to remove decimal places from data).
*/
function requestSalePrice() public returns (bytes32 requestId) 
{
Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);

// Set the URL to perform the GET request on
request.add("get", "https://api.opensea.io/api/v1/events?asset_contract_address=0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D&event_type=successful&format=json&only_opensea=false&token_id=5407");
request.add("path", "asset_events.0.total_price");

// Sends the request
return sendChainlinkRequestTo(oracle, request, fee);
}

/**
* Receive the response in the form of uint256
*/ 
function bytes32ToString(bytes32 _bytes32) public pure returns (string memory) {
uint8 i = 0;
while(i < 32 && _bytes32[i] != 0) {
i++;
}
bytes memory bytesArray = new bytes(i);
for (i = 0; i < 32 && _bytes32[i] != 0; i++) {
bytesArray[i] = _bytes32[i];
}
return string(bytesArray);
}

/**
* Receive the response in the form of uint256
*/ 
function fulfill(bytes32 _requestId, bytes32 _salePrice) public recordChainlinkFulfillment(_requestId)
{
salePrice = bytes32ToString(_salePrice);
}
}

我要访问的键是:

{
"asset_events": [
{
......
"total_price": "42000000000000000000",
......
},
{
...
}
]
}

我在路径中输入asset_events.0.total_price以获取数组asset_events(索引0)中第一项"total_price"的值。

由于某种原因,无论我尝试什么,我仍然得到0作为响应。

有什么问题吗?我该怎么补救呢?

我刚刚尝试了v0.10.11的节点(使用JSON格式),我得到了以下响应。这很可能是你没有得到响应的原因

HTTP响应太大,必须小于32768字节

对于像这个请求这样的大量JSON,我们建议创建一个外部适配器来处理请求,然后返回少量数据。例如,如果你想在API中返回5种不同的东西,适配器可以接受一个参数,根据参数,它可以返回5种不同的东西