Hi,
can someone please help me here, when i assign the values through variable it does not work. Unmarshal gives me an error. However when i assign the hard coded value it does work fine. Any specific reason please? is there any other way around to make this work please. i am using nested struct. Below is my sample code. It does work when i assign hard coded value to nested struct.
package main
import (
“encoding/json”
“fmt”
)
type TestConfig struct {
CId string json:"cid"
BSize int json:"bsize"
Number int json:"number"
LHash string json:"lhasg"
MCr string json:"mcr"
LId string json:"lid"
Fr struct {
FrId string json:"frid"
FrAdd string json:"fradd"
} json:"fr"
Ml struct {
MsId string json:"msid"
MsInfo string json:"msinfo"
} json:"ml"
}
func main() {
var a0, a1 int
var a2, a3, a4, a5, a6, a7, a8, a9 string
a0 = 1000
a1 = 10
a2 = "TestOne"
a3 = "TestTwo"
a4 = "TestThree"
a5 = "TestFour"
a6 = "TestFive"
a7 = "TestSix"
a8 = "TestSeven"
a9 = "TestEight"
jsonConfig := []byte(`{"cid" : a2,
"bsize" : a0,
"number": a1,
"lhash" : a3,
"mcr" : a4,
"lid" : a5,
"fr" :{"frid":a6,
"fradd" :a7},
"masl" :{"msid":a8,
"msinfo":a9}}`)
var config TestConfig
err := json.Unmarshal(jsonConfig, &config)
if err != nil {
panic(err)
}
fmt.Printf("Config: %+v\n", config)
}