[英]How do I preserve line breaks from Etherscan API using R and JSON?
I am using the Etherscan API to download the Solidity code for certain verified contracts. 我正在使用Etherscan API下载某些已验证合同的Solidity代码。 Whilst this does work to an extent, I lose the newline characters which I need to keep. 尽管这样做确实有效,但我丢失了需要保留的换行符。
For instance, consider the code for the verified contract on https://etherscan.io/address/0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1#code 例如,考虑https://etherscan.io/address/0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1#code上已验证合同的代码
A snippet of the Solidity code for this contract is: 该合同的Solidity代码片段为:
pragma solidity ^0.4.18;
/**
* @title SafeMath
* @dev Math operations with safety checks that throw on error
*/
library SafeMath {
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a * b;
assert(a == 0 || c / a == b);
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
more code....
Ultimately, I want the Solidity code to be stored as a string including the newlines, ie I want 最终,我希望将Solidity代码存储为包含换行符的字符串,即我想要
str_a <- "pragma solidity ^0.4.18;\\n\\n/**\\n* @title SafeMath\\n* @dev Math operations with safety checks that throw on error\\n*/\\nlibrary SafeMath {\\nfunction mul(uint256 a, uint256 b) internal pure returns (uint256) {\\nuint256 c = a * b;\\nassert(a == 0 || c / a == b);\\nreturn c;\\n}\\nfunction div(uint256 a, uint256 b) internal pure returns (uint256) {\\n// assert(b > 0); // Solidity automatically throws when dividing by 0\\nuint256 c = a / b;\\n// assert(a == b * c + a % b); // There is no case in which this doesn't hold\\nreturn c;\\n}"
However, the code I have written produces 但是,我编写的代码会产生
pragma solidity ^0.4.18;/** * @title SafeMath * @dev Math operations with safety checks that throw on error */library SafeMath { function mul(uint256 a, uint256 b) internal pure returns (uint256) { uint256 c = a * b; assert(a == 0 || c / a == b); return c; } function div(uint256 a, uint256 b) internal pure returns (uint256) { // assert(b > 0); // Solidity automatically throws when dividing by 0 uint256 c = a / b; // assert(a == b * c + a % b); // There is no case in which this doesn't hold return c; }
My code to produce the output is below, where gstr_api_key is a global string defined earlier in the code to represent the API key: 我的产生输出的代码如下,其中gstr_api_key是在代码前面定义的表示API密钥的全局字符串:
str_address <- "0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1"
# Build string needed for URL
str_APIAction <- "/api?module=contract&action=getsourcecode&address="
str_APIKey <- paste("&apikey=", gstr_api_key, sep="")
str_path <- paste(str_APIAction, str_address, str_APIKey, sep="")
# Send request to API
raw_result <- httr::GET(url = gstr_eth_url, path = str_path)
# Convert response in to useful
raw_char <- rawToChar(raw_result$content)
raw_list <- jsonlite::fromJSON(raw_char)
str_a <- raw_list$result[1]
I have limited knowledge of the JSON format, but I understand that newlines are not permitted in JSON. 我对JSON格式的了解有限,但我知道JSON中不允许换行符。 Is that correct? 那是对的吗?
How can I modify my code so that I obtain the desired output? 如何修改代码,以便获得所需的输出?
I've solved the problem - can't be done using the API. 我已经解决了问题-无法使用API完成。 Instead scrapped the page using httr and XML libraries. 而是使用httr和XML库抓取页面。 Easy enough in the end 最后很容易
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.