Go json unmarshal无法获取单个属性值

huangapple go评论85阅读模式
英文:

Go json unmarshal not picking up a single attribute value

问题

我正在尝试将一个JSON HTTP响应解组成我创建的结构体。

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 // 这个字段的值没有被更新。
    Name                string
    Rank_confidence     int
    Rank_stat           float32
    Team_id             int
    Team_name           string
    Wins                int
    Ret_msg             string
}

这是我的解组代码:

var players []Player // 使用数组是因为API返回的是一个JSON数组。
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 }

为什么我的结构体对象中MasteryLevel字段的值没有被更新?


Go Playground链接:http://play.golang.org/p/LPNiQDPx2E

英文:

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

答案1

得分: 2

Mastery_level 不等同于 MasteryLevel,只需将它们命名为相同的名称即可正常工作。

Go Playground 在这里:http://play.golang.org/p/79GfdwS5Wy

英文:

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

huangapple
  • 本文由 发表于 2013年12月11日 10:30:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/20509444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定