Once you have the JSON encoded data in a byte slice, pass those bytes into 
json.Unmarshal along with a reference to a variable of type AutoGenerated 
(or whatever you decide to rename it to). ie:

import "encoding/json"

...

   var bytes []byte
   var data AutoGenerated
   bytes = loadJSONFromDB()
   err := json.Unmarshal(bytes, &data)
   if err != nil {
      //json decoding failed
   }


It might be clearer to change AutoGenerated to be a regular struct (rather 
than a slice of structs), and define data as an array of AutoGenerated. ie:

type AutoGenerated struct {

and:

    var data []AutoGenerated

For more details check the package docs:

https://golang.org/pkg/encoding/json/#Unmarshal

-sqweek


On Thursday, September 15, 2016 at 4:12:38 PM UTC+8, Jens Ramhorst wrote:
>
> Hello,
>
> I need to create the following JSON-File:
> [{
>      "key": "Series 1",
>      "values": [
>
>                 [
>                       1051675200000,
>                  0.44227126150934
>                ],
>              [
>                       1054353600000,
>                  7.2481659343222
>         ],
>              [
>                       1056945600000,
>                  9.2512381306992
>         ],
>              [
>                       1059624000000,
>                  11.341210982529
>         ]
>       ]
> }, {
>   "key": "Series 2",
>      "values": [
>             [
>                       1025409600000,
>                  0
>               ],
>              [
>                       1028088000000,
>                  0
>               ],
>              [
>                       1030766400000,
>                  0
>               ],
>              [
>                       1033358400000,
>                  0
>               ]
>       ]
> }, {
>   "key": "Series 3",
>      "values": [
>             [
>                       1025409600000, -14.663359292964
>         ]
>       ]
> }]
>
> The JSON to Struct converter gives me the following autogenerated struct:
> type AutoGenerated []struct {
>         Key string `json:"key"`
>         Values []struct {
>                 Num0 int64 `json:"0"`
>                 Num1 float64 `json:"1"`
>         } `json:"values"`
> }
>
> How can I fill the struct dynamically. There are values from a database. 
> Sometimes I have two series with 100 Values, sometimes I have one series 
> with 30 values. or something like that.
>
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to