I am trying to write go code to parse the following json files:
{
"peers": [
{
"pid": 1,
"address": "127.0.0.1:17001"
},
{
"pid": 2,
"address": "127.0.0.1:17002"
}
]
}
What can I do so far, write this code:
package main
import (
"fmt"
"io/ioutil"
"encoding/json"
)
type Config struct{
Pid int
Address string
}
func main(){
content, err := ioutil.ReadFile("config.json")
if err!=nil{
fmt.Print("Error:",err)
}
var conf Config
err=json.Unmarshal(content, &conf)
if err!=nil{
fmt.Print("Error:",err)
}
fmt.Println(conf)
}
The above code works for non-nested json files, such as the following:
{
"pid": 1,
"address": "127.0.0.1:17001"
}
But even after the change Config structso many times. I cannot parse the json file mentioned at the beginning of the question. Can someone please tell me how to proceed?
source
share