Different approaches to solve to problem.
The simple (and safe) approach:
// import "fmt" "strings"
j := fmt.Sprintf(`{"data":{"1":%s}}`, strings.Join(strings.Fields(fmt.Sprintf("%d", results)), ","))
Previously in the comments I have made this lazier approach, without strings, but it is less safe:
// import "fmt"
j := fmt.Sprintf("%#v",*results); j = "{ \"data\": { \"1\": \""+j[7:len(j)-1]+"\"} }"
// works when the array size is between 1 and 9, above this, the "7" must increase
Now using the native golang json infrastructure.
Here, an example, using hard-coded results := []uint{0, 0, 0, 0, 0, 0}
package main
import (
"encoding/json"
"io/ioutil"
"log"
)
type d struct {
Data o `json:"data"`
}
type o struct {
One []uint `json:"1"`
}
func main() {
results := []uint{0, 0, 0, 0, 0, 0}
j, _ := json.Marshal(&d{Data:o{One:results}})
err := ioutil.WriteFile("output.json", []byte(j), 0777) // i can't test here I don't know if []byte(j) or []byte(string(j)) should be used
if err != nil {
log.Fatal(err)
}
}
But once your array is of uint8 instead of uint and golang's json encode []uint8 (which is the same as []byte) as base64 strings, we have to implement our own Marshaller to avoid this behaviour by implementing MarshalJSON, in the same way as seen on How to marshal a byte/uint8 array as json array in Go?.
package main
import (
"encoding/json"
"io/ioutil"
"strings"
"log"
"fmt"
)
type d struct {
Data o `json:"data"`
}
type o struct {
One []uint8 `json:"1"`
}
func (t *o) MarshalJSON() ([]byte, error) {
var one string
if t.One == nil {
one = "null"
} else {
one = strings.Join(strings.Fields(fmt.Sprintf("%d", t.One)), ",")
}
jsonresult := fmt.Sprintf(`{"1":%s}`, one)
return []byte(jsonresult), nil
}
func main() {
results := []uint8{0, 0, 0, 0, 0, 0}
j, _ := json.Marshal(&d{Data:o{One:results}})
err := ioutil.WriteFile("output.json", []byte(j), 0777) // i can't test here I don't know if []byte(j) or []byte(string(j)) should be used
if err != nil {
log.Fatal(err)
}
}