簡體   English   中英

json:無法將字符串解組為 map[string]interface {} 類型的 Go 值

[英]json: cannot unmarshal string into Go value of type map[string]interface {}

我一直致力於將一些加密池軟件轉換為使用不兼容的硬幣。 我相信我已經完成了這一切。 但是這個錯誤不斷出現,我似乎無法弄清楚問題是什么。 這是我的代碼:

type GetBalanceReply struct {
    Unspent string `json:"unspent"`
}

type SendTransactionReply struct {
    Hash string `json:"hash"`
}

type RPCClient struct {
    sync.RWMutex
    Url         string
    Name        string
    Account     string
    Password    string
    sick        bool
    sickRate    int
    successRate int
    client      *http.Client
}


type GetBlockReply struct {
    Difficulty       string `json:"bits"`
    Hash             string `json:"hash"`
    MerkleTreeHash   string `json:"merkle_tree_hash"`
    Nonce            string `json:"nonce"`
    PrevHash         string `json:"previous_block_hash"`
    TimeStamp        string `json:"time_stamp"`
    Version          string `json:"version"`
    Mixhash          string `json:"mixhash"`
    Number           string `json:"number"`
    TransactionCount string `json:"transaction_count"`
}

type GetBlockReplyPart struct {
    Number     string `json:"number"`
    Difficulty string `json:"bits"`
}

type TxReceipt struct {
    TxHash string `json:"hash"`
}


type Tx struct {
    Gas      string `json:"gas"`
    GasPrice string `json:"gasPrice"`
    Hash     string `json:"hash"`
}

type JSONRpcResp struct {
    Id          *json.RawMessage       `json:"id"`
    Result      *json.RawMessage       `json:"result"`
    Balance     *json.RawMessage       `json:"balance"`
    Transaction *json.RawMessage       `json:"transaction"`
    Error  map[string]interface{} `json:"error"`
}


func (r *RPCClient) GetPendingBlock() (*GetBlockReplyPart, error) {
    rpcResp, err := r.doPost(r.Url, "fetchheaderext", []interface{}{r.Account, r.Password, "pending"})
    if err != nil {
        return nil, err
    }
    if rpcResp.Result != nil {
        var reply *GetBlockReplyPart
        err = json.Unmarshal(*rpcResp.Result, &reply)
        return reply, err
    }
    return nil, nil
}

func (r *RPCClient) GetBlockByHeight(height int64) (*GetBlockReply, error) {
    //params := []interface{}{fmt.Sprintf("0x%x", height), true}
    params := []interface{}{"-t", height}
    return r.getBlockBy("fetch-header", params)
}

每當我在錢包中手動運行它時,它會顯示:

"result": {
    "bits": "7326472509313",
    "hash": "060d0f6157d08bb294ad30f97a2c15c821ff46236281f118d65576b9e4a0ba27",
    "merkle_tree_hash": "36936f36718e134e1eecaef05e66ebc4079811d8ee5a543f36d370335adc0801",
    "nonce": "0",
    "previous_block_hash": "10f4da59558fe41bab50a15864d1394462cd90836aecf52524f4cbce02a74a27",
    "time_stamp": "1520718416",
    "version": "1",
    "mixhash": "0",
    "number": "1011160",
    "transaction_count": "1"
}'

但是,代碼出錯了“json: cannot unmarshal string into Go value of type map[string]interface {}”

誰能幫我解決這個問題? 我已經花了幾個小時試圖自己解決這個問題。

如果您不確定錯誤結構,請嘗試將Error字段的類型更改為interface{} ,如果始終為string則將其更改為字符串。

這應該擺脫錯誤。

錯誤來自解組 JSONRpcResp。 輸入數據有類似{error: "this is the error message", ...} ,將其解組為 map[string]interface{} 將根本行不通。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM