manually generate json with struct fields in the correct order
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
34
main.go
34
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
@@ -8,6 +9,7 @@ import (
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -396,9 +398,37 @@ func main() {
|
||||
}
|
||||
|
||||
// Output final results in JSON
|
||||
// TODO - manually generate JSON since Marshal doesn't care about field ordering
|
||||
if len(combined) > 0 {
|
||||
j, _ := json.Marshal(combined)
|
||||
fmt.Println(string(j))
|
||||
var b []byte
|
||||
buf := bytes.NewBuffer(b)
|
||||
buf.WriteRune('{')
|
||||
l := len(combined)
|
||||
for i, entry := range combined {
|
||||
|
||||
v := reflect.ValueOf(entry)
|
||||
t := v.Type()
|
||||
|
||||
for j := 0; j < v.NumField(); j++ {
|
||||
field := v.Field(j)
|
||||
fieldType := t.Field(j)
|
||||
|
||||
km, _ := json.Marshal(fieldType.Name)
|
||||
buf.Write(km)
|
||||
buf.WriteRune(':')
|
||||
|
||||
vm, _ := json.Marshal(field.Interface())
|
||||
buf.Write(vm)
|
||||
if i != l-1 {
|
||||
buf.WriteRune(',')
|
||||
}
|
||||
}
|
||||
}
|
||||
buf.WriteRune('}')
|
||||
fmt.Println(buf.String())
|
||||
|
||||
//j, _ := json.Marshal(combined)
|
||||
//fmt.Println(string(j))
|
||||
} else {
|
||||
fmt.Println("{}")
|
||||
}
|
||||
|
Reference in New Issue
Block a user