P
P
Pavel_Timofeev2018-02-06 18:26:08
go
Pavel_Timofeev, 2018-02-06 18:26:08

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
}

The code is like this
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)
}

Why when calling
fmt.Printf("%+v", species.Result.BalanceMaps)

returns
{Mons:[]}
I need to get the Value. help me please

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
InoMono, 2018-02-06
@InoMono

Is this what you want

{
"id": "0",
"result":{
  "Maps":{
    "*Test":[
      {"Value": 6325}
    ]
  },
  "Disabled": false
},
"error": null
}

This is what the reverse operation returns - json.Marshal
{
   "id":"0",
   "result":{
      "BalanceMap":{
         "*Test":null
      },
      "Disabled":false
   },
   "error":null
}

Don't you find that they are TOO DIFFERENT?

A
Alexander Pavlyuk, 2018-02-06
@pav5000

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{}

P
Pavel_Timofeev, 2018-02-06
@Pavel_Timofeev

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)

}

fmt.PrintLn(data.Result.Maps.Test) returns [{6325}]
And fmt.PrintLn(data.Result.Maps.Test[0].Value) returns
http: panic serving 127.0.0.1:49180: runtime error: index out of range
although this code works without errors
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 question

Ask a Question

731 491 924 answers to any question