Reputation: 35
I defined struct like:
type json-input []struct {
Data string `json:"data"`
}
that Unmarshal json string like
[{"data":"some data"}, {"data":"some data"}]
data := &json-input{}
_ = json.Unmarshal([]byte(resp.Data), data)
How i can use object of this struct for turn of data
Upvotes: 3
Views: 5810
Reputation: 18201
You can't use hyphens in type declarations, and you probably want to unmarshal to resp
instead of resp.Data
; that is, you may want to do something like
import (
"encoding/json"
"fmt"
)
type jsoninput []struct {
Data string `json:"data"`
}
func main() {
resp := `[{"data":"some data"}, {"data":"some more data"}]`
data := &jsoninput{}
_ = json.Unmarshal([]byte(resp), data)
for _, value := range *data {
fmt.Println(value.Data) // Prints "some data" and "some more data"
}
}
https://play.golang.org/p/giDsPzgHT_
Upvotes: 10