简体   繁体   English

json:无法将数字解组为 Go 结构字段。字符串类型的数量

[英]json: cannot unmarshal number into Go struct field .Amount of type string

Trying to get balance of Tron wallet using tronscan.org尝试使用 tronscan.org 获取 Tron 钱包的余额

Sometimes Amount returns string, sometimes value like this有时金额返回字符串,有时像这样的值

"amount": 1.4900288469458728E-8

When it returns value of this type i get this error json: cannot unmarshal number into Go struct field.Amount of type string当它返回此类型的值时,我收到此错误json: cannot unmarshal number into Go struct field.Amount of type string

Here is my struct:这是我的结构:

type trxResponse struct {
  Data []struct {
    Amount float64 `json:"amount,string"`
  } `json:"data"`
}

How can I handle it?我该如何处理? To unmarshal json i'm using https://github.com/goccy/go-json要解组 json 我正在使用https://github.com/goccy/go-json

As the amount field has different types, it can be declared as interface{} type, this will solve unmarshaling error.由于 amount 字段有不同的类型,可以声明为interface{}类型,这样可以解决 unmarshaling 错误。

type trxResponse struct {
  Data []struct {
    Amount interface{} `json:"amount"`
  } `json:"data"`
}

This can then be typecast from/to float64 or string as needed..然后可以根据需要从/到float64string进行类型转换。

You can implement the Unmarshaler interface by declaring custom UnmarshalJSON method, which will handle the amount field according to the type.您可以通过声明自定义UnmarshalJSON方法来实现Unmarshaler接口,该方法将根据类型处理金额字段。


type trxData struct {
    Amount float64 `json:"amount"`
}

type trxResponse struct {
    Data []trxData `json:"data"`
}

// UnmarshalJSON custom method for handling different types
// of the amount field.
func (d *trxData) UnmarshalJSON(data []byte) error {
    var objMap map[string]*json.RawMessage

    // unmarshal json to raw messages
    err := json.Unmarshal(data, &objMap)
    if err != nil {
        return err
    }

    var amount float64 // try to unmarshal to float64
    if rawMsg, ok := objMap["amount"]; ok {
        if err := json.Unmarshal(*rawMsg, &amount); err != nil {
            // if failed, unmarshal to string
            var amountStr string
            if err := json.Unmarshal(*rawMsg, &amountStr); err != nil {
                return err
            }
            amount, err = strconv.ParseFloat(amountStr, 64)
            if err != nil {
                return err
            }
        }
    }

    d.Amount = amount

    return nil
}

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

相关问题 Terraform 使用模式创建 BigQuery 表 - 错误:json:无法将数组解组为 Go 字符串类型的结构字段 TableFieldSchema.type - Terraform BigQuery table creeation with schema - Error: json: cannot unmarshal array into Go struct field TableFieldSchema.type of type string 将 map[string]DynamoDBAttributeValue 解组为结构 - Unmarshal map[string]DynamoDBAttributeValue into a struct 尝试更新 Go 中的结构时对字段的无效分配 - Ineffective Assignment to Field when trying to update a Struct in Go 如何在golang中获取结构的json字段名称? - How to get the json field names of a struct in golang? 如何正确解组/解析使用 Go 的请求? - How to unmarshal/parse a request using Go correctly? Golang json 解组“JSON 输入的意外结束” - Golang json Unmarshal "unexpected end of JSON input" GO 结构体参数接口 - GO struct parameters interface 更新 JSON 字符串中的数组字段的值 - Update value of array field inside JSON string Firebase 文档参考字段设置为字符串字段类型而不是参考字段类型 - Firebase document reference field gets set as string field type instead of reference field type 为什么这些 golang JSON Unmarshal 示例的行为不同? - Why do these golang JSON Unmarshal examples behave differently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM