【问题标题】:json: cannot unmarshal string into Go value of type map[string]interface {}json:无法将字符串解组为 map[string]interface {} 类型的 Go 值
【发布时间】:2018-08-19 05:55:33
【问题描述】:

我一直致力于将一些加密池软件转换为使用不兼容的硬币。我相信我已经完成了一切。但是这个错误不断出现,我似乎无法弄清楚问题是什么。这是我的代码:

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 {}”

谁能帮我解决这个问题?我已经花了好几个小时试图自己解决这个问题。

【问题讨论】:

  • 只是猜测,但您定义的唯一 map[string]interface{}Error 字段的类型。然后,错误消息将意味着您返回的 json 包含一个 "error" 键,其值不是 json 对象而是字符串。查看JSONRpcResp 似乎"error" 键是"result" 键的兄弟,所以没有向我们展示"result" 是一个孩子的json 数据,我无法确定。
  • 结果应该来自我发布的JSON数据吧?这再次是: “结果”:{ “位”: “7326472509313”, “散列”: “060d0f6157d08bb294ad30f97a2c15c821ff46236281f118d65576b9e4a0ba27”, “merkle_tree_hash”: “36936f36718e134e1eecaef05e66ebc4079811d8ee5a543f36d370335adc0801”, “随机数”: “0”, “previous_block_hash”: “10f4da59558fe41bab50a15864d1394462cd90836aecf52524f4cbce02a74a27”, “time_stamp”:“1520718416”,“version”:“1”,“mixhash”:“0”,“number”:“1011160”,“transaction_count”:“1”}'
  • @Adroitful 你确定错误不是从rpcResp, err := r.doPost返回的吗?但它来自err = json.Unmarshal(*rpcResp.Result, &reply)
  • 嗯,错误表明尝试将映射解组为字符串时出现问题。所以我假设它来自 json.Unmarshal 。我对这门语言很陌生,它让我很头疼。可能只是要放弃这个项目lol
  • 所以你是说doPost 不调用json.Unmarshal?您可以在这两个返回语句中的每一个之前放置一个日志语句吗?或者试试这个,注释掉 JSONRpcResp 中的 Error 字段。

标签: json go


【解决方案1】:

如果您不确定错误结构,请尝试将Error 字段的类型更改为interface{},或者如果它始终是字符串,则将其更改为string

这应该可以消除错误。

错误来自解组 JSONRpcResp。输入数据有 {error: "this is the error message", ...} 之类的东西,将其解组到 map[string]interface{} 根本行不通。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-04
    • 2021-03-05
    • 2021-01-05
    • 2018-08-28
    相关资源
    最近更新 更多