3

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

1 Answer 1

10

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_

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.