Go-Ethereum

geth 中,我如何將 abi 對象轉換為字元串?

  • January 4, 2017

在裡面geth如何將 abi 對象轉換為字元串?我想將其轉換為字元串以應用於str.replace(/[\r\n]/g, '');刪除新行並使其更緊湊。

myContractCompiled.Array.info.abiDefinition

返回合約的 abi 為:

[{
   constant: true,
   inputs: [{
       name: "id",
       type: "string"
   }, {
       name: "index",
       type: "uint256"
   }],
   name: "get_length",
   outputs: [{
       name: "",
       type: "uint256"
   }],
   payable: false,
   type: "function"
}, 
...
{
   inputs: [],
   payable: false,
   type: "constructor"
}]

但是當我嘗試將 abi 對象轉換為字元串時遇到以下錯誤:

> myContractCompiled.Array.info.abiDefinition.toString()
"[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

感謝您寶貴的時間和幫助。

> JSON.stringify(myContractCompiled.Array.info.abiDefinition)
"[{\"constant\":true,\"inputs\":[],\"name\":\"test_callstack\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"ownerOnly\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"owner\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"newOwner\",\"type\":\"address\"}],\"name\":\"transferOwnership\",\"outputs\":[],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"type\":\"constructor\"}]"

另請參閱:JSON.stringify()

引用自:https://ethereum.stackexchange.com/questions/11099