I’m getting this information from a JSON file:
{
"information": [
{
"type": "general",
"news": [
{ "name": "abc", "read": true },
{ "name": "def", "read": true }
]
},
{
"sort": "confidential",
"information": [
{ "name": "xxx", "read": false },
{ "name": "yyy", "read": false }
]
}
]
}
And that is the perform used to extract the JSON info
func ReadFile(file_name string) [ ][ ]string {
var getInfo [ ][ ]string
sort Information struct {
Title string `json:"identify"`
Learn bool `json:"learn"`
}
sort Data struct {
Sort string `json:"sort"`
Information Information `json:"information"`
}
sort Data struct {
Data [ ]Data `json:"information"`
}
// Open file
jsonFile, err := os.Open(file_name)
if err != nil {
fmt.Println(err)
}
fmt.Println("Efficiently Opened file_name.json")
// defer the closing of our jsonFile in order that we will parse it in a while
defer jsonFile.Shut()
// learn our opened file_name as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
var infor Data
// we unmarshal our byteArray which comprises our
// jsonFile's content material into 'Infor' which we outlined above
json.Unmarshal(byteValue, &infor)
for i := 0; i < len(infor.Data); i++ {
getInfo[i] = append(getInfo[i], infor.Data[i].Sort)
for j := 0; j < len(infor.Data.Information); j++ {
getInfo[i][j] = append(getInfo, infor.Data[i].Information[j].Title)
getInfo[i][j+1] = append(getInfo, infor.Data[i].Information[j].Learn)
}
}
return getInfo
}
I’m not certain if that is one of the best ways to extract the JSON info as I’ve completely different “sorts” and appears like I can’t append like that as I’m getting an error relating to the kind of variable i’m making an attempt to append.
Principally I’m making an attempt to get this information into the getInfo var:
getInfo = [general][abc, true, def, true]
[confidential][xxx, false, yyy, false]
- You’ll be able to simply unmarshal your JSON right into a
[]Data
.
is just not what a [][]string
would appear like. If you happen to really do need a [][]string
, possibly you imply for it to look one thing like:
`[[“general”,“abc, true, def, true”], [“confidential”,“xxx, false, yyy, false”]
- I like to recommend you unmarshal as in 1) and rework the
[]Data
to your required sort(s).
Thanks to your reply.
Lastly I’m doing one thing like this however it’s complaining that “Environments” is undeclared within the ReadFile perform.
I declared the Environments struct in the principle perform and simply passing the filename as an argument when calling the ReadFile perform:
func predominant () {
sort Data struct {
Data []Data `json:"information"`
}
sort Data struct {
Sort string `json:"sort"`
Information []New `json:"information"`
}
sort New struct {
Title string `json:"identify"`
Learn bool `json:"learn"`
}
file_name := "file.json"
studying := ReadFile(file_name)
That is the ReadFile perform:
func ReadFile(file_name string) *Data {
jsonFile, err := os.Open(file_name)
if err != nil {
fmt.Println(err)
}
fmt.Println("Efficiently Opened file.json")
defer jsonFile.Shut()
byteValue, _ := ioutil.ReadAll(jsonFile)
var infor Data
json.Unmarshal(byteValue, &infor)
return &infor
}
I’m unsure if it is a runtime error.
You don’t point out the issue. Your code compiles for me, it should be a runtime error. I count on it is because of your ignoring errors.
Please, test it out once more. I edited the message.
It’s already resolved. I needed to declare the struct outdoors the principle perform.
1 Like
This subject was routinely closed 90 days after the final reply. New replies are not allowed.