fixed printArray function

This commit is contained in:
Nils Stinnesbeck 2020-10-14 16:23:50 +02:00
parent a2031d2e0c
commit 7ab3b8d2b1
Signed by: nils
GPG Key ID: 86D4882C6C6CA48B

30
main.go
View File

@ -10,15 +10,17 @@ import (
// Print will beautify the output of body
func Print(body interface{}) {
b, err := json.Marshal(body)
if err != nil {
panic(err)
}
var obj map[string]interface{}
//json.Unmarshal([]byte(body), &obj)
json.Unmarshal(b, &obj)
// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 2
// Marshall the Colorized JSON
// Marshal the Colorized JSON
s, err := f.Marshal(obj)
if err != nil {
panic(err)
@ -29,8 +31,26 @@ func Print(body interface{}) {
}
// PrintArray will beautify the output of an interface array (bodyArray)
func PrintArray(bodyArray []interface{}) {
for i := range bodyArray {
Print(bodyArray[i])
func PrintArray(bodyArray interface{}) {
b, err := json.Marshal(bodyArray)
if err != nil {
panic(err)
}
var obj []map[string]interface{}
json.Unmarshal(b, &obj)
// Make a custom formatter with indent set
f := colorjson.NewFormatter()
f.Indent = 2
for i := range obj {
// Marshal the Colorized JSON
s, err := f.Marshal(obj[i])
if err != nil {
panic(err)
}
// Print out string to stdout
fmt.Println(string(s))
}
}