I assume that params
depends on the method
. If so, start by decoding the top-level to a struct with the method name and a json.RawMessage to collect the parameters as JSON text.
var call struct {
Method string
Params json.RawMessage
}
if err := json.Unmarshal(data, &call); err != nil {
log.Fatal(err)
}
Declare variables for each parameter. This code will depend on the method.
var param0 int
var param1 []struct {
Previous string
Timestamp string
Witness string
Transaction_merkle_root string
Extensions []interface{}
Witness_signature string
}
Create a parameter list using pointers to those variables.
params := []interface{}{¶m0, ¶m1}
Now unmarshal the JSON to params. This will set the variables param0 and param1:
if err := json.Unmarshal(call.Params, ¶ms); err != nil {
log.Fatal(err)
}
Repeat for other methods.
Here's the runnable code on the playground.
This can be simplified if you will only need to handle the "notice" method. In this case, you can decode directly to the parameters without the RawMessage step.
var param0 int
var param1 []struct {
Previous string
Timestamp string
Witness string
Transaction_merkle_root string
Extensions []interface{}
Witness_signature string
}
call := struct {
Method string
Params []interface{}
}{
Params: []interface{}{¶m0, ¶m1},
}
if err := json.Unmarshal(data, &call); err != nil {
log.Fatal(err)
}
Here's runnable code for the simple case.