Use a slice of maps instead of strings, as you have key-value pairs of strings.
type ExampleStruct struct {
Added []map[string]string `json:"add"`
}
Here is a full example:
package main
import (
"bytes"
"encoding/json"
"fmt"
)
func main() {
const code = `{
"add":[{"id": "1234ABCD"}]
}`
type ExampleStruct struct {
Added []map[string]string `json:"add"`
}
var data ExampleStruct
json.NewDecoder(bytes.NewReader([]byte(code))).Decode(&data)
fmt.Println(data)
}
EDIT
Since you want to have only the values of the maps, here is a complete example where Added is a function that can be called on the ExampleStruct. It assumes that each map only contains two strings (id and value):
package main
import (
"bytes"
"encoding/json"
"fmt"
)
func main() {
const code = `{
"add":[{"id": "1234ABCD"}, {"id": "5678EFGH"}]
}`
var data ExampleStruct
json.NewDecoder(bytes.NewReader([]byte(code))).Decode(&data)
fmt.Println(data)
fmt.Println(data.Added())
}
type ExampleStruct struct {
Add []map[string]string `json:"add"`
}
func (e ExampleStruct) Added() []string {
values := make([]string, len(e.Add))
for i := range e.Add {
for _, v := range e.Add[i] {
values[i] = v
}
}
return values
}
idif you're using only first item? Why no add a method toExampleStructand fetch first item.idthat could be added from the API call. I just added one for brevity.ExampleStruct.Addedyou still get the first item?ExampleStruct.Added, I should get a slice of strings in this format:["1234ABCD", "5678EFGH"]Addedis not a function, you cannot call it.