Last active
November 11, 2022 09:28
-
-
Save abtswath/9d288c96b22d93cded720486e20d5ed6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"log" | |
) | |
var s = ` | |
[{ | |
"video": [ | |
1, | |
2, | |
3 | |
], | |
"video_type": "template_a" | |
}, | |
{ | |
"video": { | |
"a": { | |
"aa": [] | |
} | |
}, | |
"video_type": "template_b" | |
}] | |
` | |
type sliceVideo []int | |
type objectVideo struct { | |
A struct { | |
Aa []any `json:"aa"` | |
} `json:"a"` | |
} | |
type template struct { | |
VideoType string `json:"video_type"` | |
VideoRaw json.RawMessage `json:"video"` | |
Video interface{} | |
} | |
func main() { | |
buf := bytes.NewBuffer([]byte(s)) | |
decoder := json.NewDecoder(buf) | |
var templates []*template | |
err := decoder.Decode(&templates) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
for _, t := range templates { | |
switch t.VideoType { | |
case "template_a": | |
t.Video = &sliceVideo{} | |
case "template_b": | |
t.Video = &objectVideo{} | |
} | |
err := json.Unmarshal(t.VideoRaw, &t.Video) | |
if err != nil { | |
log.Fatalln(err) | |
} | |
} | |
for _, v := range templates { | |
fmt.Printf("%s: %v\n", v.VideoType, v.Video) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment