How to Unmarshal nested JSON structure?
Example
package main
import (
"encoding/json"
"fmt"
)
func main() {
jStr := `
{
"AAA":{
"BBB": {
"CCC": ["1111"],
"DDD": ["2222", "3333"]
}
}
}
`
type Inner struct {
Key2 []string `json:"CCC"`
Key3 []string `json:"DDD"`
}
type Outer struct {
Key Inner `json:"BBB"`
}
type Outmost struct {
Key Outer `json:"AAA"`
}
var cont Outmost
json.Unmarshal([]byte(jStr), &cont)
fmt.Printf("%+v\n", cont)
}
Output
{Key:{Key:{Key2:[1111] Key3:[2222 3333]}}}
Most Helpful This Week
Convert specific UTC date time to PST, HST, MST and SGT
Regular expression to extract numbers from a string in Golang
How To Make an HTTP Server in Golang?
How to read names of all files and folders in current directory?
Passing multiple string arguments to a variadic function
Golang String Concatenation
How to read/write from/to file in Golang?
How can we reverse a simple string in Go?
How to blur an image in Golang?
Replace numbers by zero from string