Reputation: 137
{
"meta": {
"type": "RESPONSE",
"application": "",
"data0": {
some data
},
"lv1": [
{
"status": "SUCCESS",
"response-type": "JSON",
"message": {},
"response": {
"more_data": "TRUE",
"no_result": "5",
"current_page": "1",
"data": [[
"1",
"2",
"3"]]
}
}
]
}
}
type response struct {
META struct {
LV []struct {
RESPONSE struct {
Data []struct {
array []struct {
val []string
}
} `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
How can I get the values in the following?
"data": [[
"1",
"2",
"3"]]
I've tried both interface and struct. Using interface results in [1 2 3]
of interface type and I'm not sure how I can get the values. When using struct, I ran into problem when trying to map an array of array with error message:
"cannot unmarshal array into Go struct field .data of type struct { vals []string }"
Upvotes: 3
Views: 3942
Reputation: 46432
It's an array of arrays of strings, not an array of structs containing arrays of structs containing strings, so you want something more like:
type response struct {
Meta struct {
Lv []struct {
Response struct {
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
(I also changed the all-caps field names to match expected Go code style.)
For what it's worth, there is a handy JSON-to-Go tool here which gave me this for your input, after deleting the some data
bit (which made the JSON invalid):
type AutoGenerated struct {
Meta struct {
Type string `json:"type"`
Application string `json:"application"`
Data0 struct {
} `json:"data0"`
Lv1 []struct {
Status string `json:"status"`
ResponseType string `json:"response-type"`
Message struct {
} `json:"message"`
Response struct {
MoreData string `json:"more_data"`
NoResult string `json:"no_result"`
CurrentPage string `json:"current_page"`
Data [][]string `json:"data"`
} `json:"response"`
} `json:"lv1"`
} `json:"meta"`
}
Upvotes: 15