I'm just starting to learn go.
This is the file I need to parse:
{
"people": {
"asd": {
"name": "Alex",
"id": 0,
"s": "m"
},
"fda": {
"name": "Mike",
"id": 1,
"s": "m"
},
"fg2": {
"name": "Rosa",
"id": 3,
"s": "f",
"Childs" :
[
{
"name": "Bob",
"age": 1,
"s": "m"
},
{
"name": "Maria",
"age": 2,
"s": "f"
}
]
}
}
}
asd, fda, fg2 are some kind of identifiers consisting of a random set of characters.
First of all, I couldn't describe the structure for this file, I was confused by these random identifiers.
And I did this:
type people {
People *map[string]interface{} `json:"people"`
}
So I'm trying to read and parse this file and just got stuck. This is my code:
package main
import "encoding/json"
import "io/ioutil"
import "log"
import "fmt"
type people struct {
People map[string]interface{} `json:"people"`
}
func main() {
file, err := ioutil.ReadFile("./people.json")
if err != nil {
log.Println("error:", err)
}
var data people
err = json.Unmarshal([]byte(file), &data)
if err != nil {
log.Println("error:", err)
}
//fmt.Println(data.people)//
fmt.Println(data)
// for i := 0; i < len(data); i++ {//here the compiler throws error...
// fmt.Println("name: ", data[i].name)//#@!o_0
// }
}
The error is: invalid operation: data[i] (type people does not support indexing).
Please help me sort out and print the names of these people, as well as the names of their children.
Alex
Mike
Rosa
Bob
Maria