I’m getting this info from a JSON file:
{
"info": [
{
"type": "general",
"news": [
{ "name": "abc", "read": true },
{ "name": "def", "read": true }
]
},
{
"type": "confidential",
"news": [
{ "name": "xxx", "read": false },
{ "name": "yyy", "read": false }
]
}
]
}
And this is the function used to extract the JSON information
func ReadFile(file_name string) [ ][ ]string {
var getInfo [ ][ ]string
type News struct {
Name string `json:"name"`
Read bool `json:"read"`
}
type Info struct {
Type string `json:"type"`
News News `json:"news"`
}
type Information struct {
Info [ ]Info `json:"info"`
}
// Open file
jsonFile, err := os.Open(file_name)
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened file_name.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened file_name as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
var infor Information
// we unmarshal our byteArray which contains our
// jsonFile's content into 'Infor' which we defined above
json.Unmarshal(byteValue, &infor)
for i := 0; i < len(infor.Information); i++ {
getInfo[i] = append(getInfo[i], infor.Information[i].Type)
for j := 0; j < len(infor.Information.News); j++ {
getInfo[i][j] = append(getInfo, infor.Information[i].News[j].Name)
getInfo[i][j+1] = append(getInfo, infor.Information[i].News[j].Read)
}
}
return getInfo
}
I am not sure if this is the best way to extract the JSON information as I have different “types” and looks like I cannot append like that as I’m getting an error regarding the type of variable i’m trying to append.
Basically I’m trying to get this info into the getInfo var:
getInfo = [general][abc, true, def, true]
[confidential][xxx, false, yyy, false]