Answer the question
In order to leave comments, you need to log in
When parsing a json object in golang, emptiness is returned?
Good day to all.
I'm trying to parse the following json
{
"id": "0",
"result":{
"Maps":{
"*Test":[
{"Value": 6325}
]
},
"Disabled": false
},
"error": null
}
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`
{"id": "0",
"result":{
"Maps":{
"*Test":[
{"Value": 6325}
]
},
"Disabled": false
},
"error": null
}`)
type Mon struct {
Value interface{} `json:"Value"`
}
type BalanceMap struct {
Mons []Mon `json:"*Test"`
}
type Result struct {
BalanceMaps BalanceMap `json:"BalanceMap"`
Disabled interface{} `json:"Disabled"`
}
type Response struct {
Id interface{} `json:"id"`
Result Result `json:"result"`
Error interface{} `json:"error"`
}
var species Response
err := json.Unmarshal(jsonBlob, &species)
if err != nil {
fmt.Println("error:", err)
}
fmt.Printf("%+v", species.Result.BalanceMaps)
}
fmt.Printf("%+v", species.Result.BalanceMaps)
Answer the question
In order to leave comments, you need to log in
Is this what you want
{
"id": "0",
"result":{
"Maps":{
"*Test":[
{"Value": 6325}
]
},
"Disabled": false
},
"error": null
}
{
"id":"0",
"result":{
"BalanceMap":{
"*Test":null
},
"Disabled":false
},
"error":null
}
You made a mistake with the annotation
. For some reason you have "BalanceMap" there.
PS And it's strange that Disabled's type is not bool, but interface{}
yes I stepped with the structure. I did it right, but now for some reason if I get data from a function
type AutoGenerated struct {
ID string `json:"id"`
Result struct {
Maps struct {
Test []struct {
Value int `json:"Value"`
} `json:"*Test"`
} `json:"Maps"`
Disabled bool `json:"Disabled"`
} `json:"result"`
Error interface{} `json:"error"`
}
func getTest(param1 string) string {
var jsonStr = []byte(fmt.Sprintf(`{"id":"0", "jsonrpc":"2.0", "method":"getTest", "params":[{"Param":"%s"}]}`, param1))
req, err := http.NewRequest("POST", "Some Url", bytes.NewBuffer(jsonStr))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Error(err)
panic(err.Error())
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
var data AutoGenerated
err = json.Unmarshal(body, &data)
fmt.PrintLn(data.Result.Maps.Test[0].Value)
fmt.PrintLn(data.Result.Maps.Test)
}
package main
import (
"encoding/json"
"fmt"
)
func main() {
var jsonBlob = []byte(`
{"id": "0",
"result":{
"Maps":{
"*Test":[
{"Value": 6325}
]
},
"Disabled": false
},
"error": null
}`)
type AutoGenerated struct {
ID string `json:"id"`
Result struct {
Maps struct {
Test []struct {
Value int `json:"Value"`
} `json:"*Test"`
} `json:"Maps"`
Disabled bool `json:"Disabled"`
} `json:"result"`
Error interface{} `json:"error"`
}
var species AutoGenerated
err := json.Unmarshal(jsonBlob, &species)
if err != nil {
fmt.Println("error:", err)
}
fmt.PrinДт(species.Result.Maps.Test[0].Value)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question