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
Replace first occurrence of string using Regexp
How to concatenate two or more slices in Golang?
How to add Watermark or Merge two image?
How to read current directory using Readdir?
How to use a mutex to define critical sections of code and fix race conditions?
The return values of a function can be named in Golang