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
Regular expression for matching HH:MM time format in Golang
Get Hours, Days, Minutes and Seconds difference between two dates [Future and Past]
How to iterate over an Array using for loop?
Various examples of Carbon date-time package in Golang
Find element in a slice and move it to first position?
Example: How to use ReadAtLeast from IO Package in Golang?
How to fetch an Integer variable as String in Go?
Golang Slice interface and array concatenation
Print index and element or data from Array, Slice and Map
Regular expression to validate phone number