简体   繁体   English

json:无法将字符串解组为 map[string]interface {} 类型的 Go 值

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

I have been working on converting some crypto pool software over to work with an incompatible coin.我一直致力于将一些加密池软件转换为使用不兼容的硬币。 I believe I have it all about done.我相信我已经完成了这一切。 But this error keeps popping up and I just can't seem to figure out what the problem is.但是这个错误不断出现,我似乎无法弄清楚问题是什么。 Here is my code:这是我的代码:

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

Whenever I run that manually in the wallet it displays:每当我在钱包中手动运行它时,它会显示:

"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"
}'

But, the code errors out with "json: cannot unmarshal string into Go value of type map[string]interface {}"但是,代码出错了“json: cannot unmarshal string into Go value of type map[string]interface {}”

Can anyone help me figure this one out?谁能帮我解决这个问题? I've been hours on trying to figure it out on my own.我已经花了几个小时试图自己解决这个问题。

Try changing the type of the Error field to interface{} if you are not sure about the error structure, or change it to string if its always a string.如果您不确定错误结构,请尝试将Error字段的类型更改为interface{} ,如果始终为string则将其更改为字符串。

This should get rid of the error.这应该摆脱错误。

The error comes from unmarshaling JSONRpcResp.错误来自解组 JSONRpcResp。 The input data has something like {error: "this is the error message", ...} and unmarshaling this into a map[string]interface{} will simply not work.输入数据有类似{error: "this is the error message", ...} ,将其解组为 map[string]interface{} 将根本行不通。

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

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