There was an error: math/big: cannot unmarshal into a *big.Int

There was an error: math/big: cannot unmarshal into a *big.Int

我正在尝试将 json 字符串解组为 golang 中的大整数。我收到以下错误。还有另一种方法可以使它起作用吗?

There was an error: math/big: cannot unmarshal "\"82794247871852158897004947856472973914188862150580220767211643167985440428259\"" into a *big.Int

代码:

游乐场:https://play.golang.org/p/F5RMehTau8e

package main

import (
    "fmt"
    "math/big"
    "encoding/json"
)


type Signature struct {
    R, S *big.Int
    V, O uint8 // V is a reconstruction flag and O a multi sig order
}


func main() {

    string := []byte(`{"O":0,"R":"82794247871852158897004947856472973914188862150580220767211643167985440428259","S":"39475619887140601172207943363731402979187092853596849493781395367115389948109","V":0}`)   

    var sig Signature

    err2 := json.Unmarshal([]byte(string), &sig)
    if err2 != nil {
        fmt.Println("There was an error:", err2)
    }
    fmt.Println("r", sig.R, "s", sig.S, "o", sig.O, "v", sig.V)

}

@d3t0x!请看

big#Int.SetString

可能的方式:

package main

import (
    "encoding/json"
    "fmt"
    "math/big"
)

type Signature struct {
    R, S BigInt
    V, O uint8 // V is a reconstruction flag and O a multi sig order
}

type BigInt struct {
    big.Int
}

func (i *BigInt) UnmarshalJSON(b []byte) error {
    var val string
    err := json.Unmarshal(b, &val)
    if err != nil {
        return err
    }

    i.SetString(val, 10)

    return nil
}

func main() {
    string := []byte(`{"O":0,"R":"82794247871852158897004947856472973914188862150580220767211643167985440428259","S":"39475619887140601172207943363731402979187092853596849493781395367115389948109","V":0}`)

    var sig Signature

    err2 := json.Unmarshal([]byte(string), &sig)
    if err2 != nil {
        fmt.Println("There was an error:", err2)
    }

    fmt.Printf("r %s s %s o %d v %d", sig.R.String(), sig.S.String(), sig.O, sig.V)
}

游乐场:

1) https://play.golang.org/p/Qp-hiiPDfZM

2) 在此处草拟并不太干净的解决方案 https://play.golang.org/p/YYdf85ub5-T