2017-10-18 7 views
0

Im essayant de unmarshall les éléments suivants JSON dans un struct, mais je suis incapable de traduire le contenu du champ de valeurs avec le [[int,string]] C'est ce que j'ai tellement loin:unmarshall JSON avec une carte de string [int] contenue dans un tableau

type Response struct { 
      Metric struct { 
       Name string `json:"name,omitempty"` 
       Appname string `json:"appname,omitempty"` 
      } `json:"metric,omitempty"` 
      Values []map[int]string `json:"values,omitempty"` 
} 

Le fichier JSON:

{ 
    "metric":{ 
     "name":"x444", 
     "appname":"cc-14-471s6" 
    }, 
    "values":[ 
     [ 
     1508315264, 
     "0.0012116165566900816" 
     ], 
     [ 
     1508315274, 
     "0.0011871631158857396" 
     ] 
    ] 
} 

Répondre

1

Les données que vous montriez doit être démarshalées à:

type Response struct { 
      Metric struct { 
       Name string `json:"name,omitempty"` 
       Appname string `json:"appname,omitempty"` 
      } `json:"metric,omitempty"` 
      Values [][]interface{} `json:"values,omitempty"` 
} 

Si vous voulez transférer à la carte mettre en œuvre l'interface json.Unmarshaller-https://golang.org/pkg/encoding/json/#Unmarshaler

Vous pouvez avoir quelque chose comme:

type Item struct { 
    Key int 
    Val string 
} 
func(item *Item) UnmarshalJSON([]byte) error { 
    // TODO: implement 
} 

type Response struct { 
      Metric struct { 
       Name string `json:"name,omitempty"` 
       Appname string `json:"appname,omitempty"` 
      } `json:"metric,omitempty"` 
      Values []Item `json:"values,omitempty"` 
}