繁体   English   中英

如何使用R和JSON保留Etherscan API的换行符?

[英]How do I preserve line breaks from Etherscan API using R and JSON?

我正在使用Etherscan API下载某些已验证合同的Solidity代码。 尽管这样做确实有效,但我丢失了需要保留的换行符。

例如,考虑https://etherscan.io/address/0xb494e1195e0c6bad435be4c9fa0b36ce94bfd7d1#code上已验证合同的代码

该合同的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....

最终,我希望将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}"

但是,我编写的代码会产生

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; }

我的产生输出的代码如下,其中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] 

我对JSON格式的了解有限,但我知道JSON中不允许换行符。 那是对的吗?

如何修改代码,以便获得所需的输出?

我已经解决了问题-无法使用API​​完成。 而是使用httr和XML库抓取页面。 最后很容易

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM