0

What im doing wrong? I tried tons of way but i get always empty array. Here is my code:

type Switches struct {
    switches []SwitchInfo
}

type SwitchInfo struct {
    IP        string
    Community string
}

func main() {
    file, e := ioutil.ReadFile("./config.json")

    if e != nil {
        fmt.Printf("File error: %v\n", e)
        os.Exit(1)
    }

    fmt.Printf("%s\n", file)

    var jsonObject Switches

    err := json.Unmarshal(file, &jsonObject)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Results: %v\n", jsonObject)

}

And this is my json file

{"switches":
[
    {
    "IP" : "1.1.1.1",
    "Community" : "asdfg"
    },
    {
    "IP" :"1.1.1.1",
    "Community" : "asdf"
    }
]
}

I checked my json file it is valid. I think i have problem with my structs. Ty for your help.

2
  • 1
    Has been asked a million times. You have to export the fields of the struct you're marshalling into. Commented Feb 23, 2016 at 9:38
  • omg! ty sir that mistake take my time horribly. I examined other questions about this but i thougth i was doing everything right sorry for my inattention Commented Feb 23, 2016 at 9:43

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.