1

I just started developing in Golang, and am up against this problem:

I have json data to be accepted by a Golang server. A sample of one of the fields of this json data is below:

 "DATA":[["(610, 658)",1573824148],["(594, 675)",1573824148],["(578,710)",1573824148],["(571, 728)",1573824148],["(552, 769)",1573824148],["(549, 788)",1573824148],["(549, 796)",1573824148]]

Note that it is an array of lists, each list being a string and an integer.

I have the following data structures declared:

type POINT struct {
    p    string
    t    int
}

type POINTS struct {
    A         int
    B         int
    C         string
    DATA     []FIDGET
}

The relevant field is POINTS.DATA which is an array of POINTs. A POINT is a string and an int. To convert from json to Go variables, I use the encoding/json package.

My problem is, when I call

err = json.Unmarshal(body, &m)

I get this error:

json: cannot unmarshal array into Go struct field POINTS.DATA of type main.POINT

Can anyone help me? Thank you in advance!

2
  • Btw, "body" is the request's body, extracted this way:body, err := ioutil.ReadAll(req.Body) where req is the httpRequest. Commented Nov 15, 2019 at 14:22
  • 1
    You don't have the definition for FIDGET, or for the FIDGETS show in the error message. Commented Nov 15, 2019 at 14:23

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.