Question

I'm trying to unmarshal a JSON http response into a Struct I created.

JSON from the API endpoint:

[{"Created_Datetime":"2\/16\/2013 2:57:59 AM",
  "Last_Login_Datetime":"11\/27\/2013 11:00:49 PM",
  "Leaves":0,
  "Level":18,
  "Losses":47,
  "MasteryLevel":2,
  "Name":"sergiotapia",
  "Rank_Confidence":0,
  "Rank_Stat":0,
  "TeamId":121147,
  "Team_Name":"sergiosteam",
  "Wins":65,
  "ret_msg":null}]

And my struct:

type Player struct {
    Created_datetime    string
    Last_login_datetime string
    Leaves              int
    Level               int
    Losses              int
    Mastery_level       int // This is the one that isn't being updated with the value.
    Name                string
    Rank_confidence     int
    Rank_stat           float32
    Team_id             int
    Team_name           string
    Wins                int
    Ret_msg             string
}

Here's how I'm unmarshaling:

var players []Player // Using an array because the API returns a JSON array - always.
json.Unmarshal(httpResponse, &players)
return players[0]

Resulting in:

// fmt.Println(player)
{2/16/2013 2:57:59 AM 11/27/2013 11:00:49 PM 0 18 47 0 sergiotapia 0 0 0 sergiosteam 65 }

Why is the value for MasterLevel not being updated in my struct object?


Go Playground here: http://play.golang.org/p/LPNiQDPx2E

Was it helpful?

Solution

Mastery_level is not the same as MasteryLevel, just name them the same and it works.

Go Playground here: http://play.golang.org/p/79GfdwS5Wy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top