去json unmarshal没有获取单个属性值

时间:2013-12-11 02:30:08

标签: json struct go unmarshalling

我正在尝试将JSON http响应解组为我创建的Struct。

来自API端点的JSON:

[{"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}]

我的结构:

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
}

以下是我如何解组:

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

导致:

// 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 }

为什么MasterLevel的值没有在我的struct对象中更新?


在这里去游乐场:http://play.golang.org/p/LPNiQDPx2E

1 个答案:

答案 0 :(得分:2)

Mastery_levelMasteryLevel不同,只需将它们命名为相同即可。

在这里去游乐场:http://play.golang.org/p/79GfdwS5Wy