call json keys undeclared in golang

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

call json keys undeclared in golang

问题

假设我有以下结构体:

type LstBQ4422400 struct {
	Sku               string  `json:"sku"`
	Image             string  `json:"image"`
	Nom               string  `json:"Nom"`
	Prix              float64 `json:"prix"`
	Num00195866137462 string  `json:"00195866137462"`
	Num00195866137479 string  `json:"00195866137479"`
	Num00195866137486 string  `json:"00195866137486"`
	Num00195866137493 string  `json:"00195866137493"`
	Num00195866137509 string  `json:"00195866137509"`
	Num00195866137516 string  `json:"00195866137516"`
	Num00195866137523 string  `json:"00195866137523"`
	Num00195866137530 string  `json:"00195866137530"`
	Num00195866137547 string  `json:"00195866137547"`
	Num00195866137554 string  `json:"00195866137554"`
	Num00195866137561 string  `json:"00195866137561"`
	Num00195866137578 string  `json:"00195866137578"`
	Num00195866137585 string  `json:"00195866137585"`
	Num00195866137592 string  `json:"00195866137592"`
	Num00195866137608 string  `json:"00195866137608"`
	Num00195866137615 string  `json:"00195866137615"`
	Num00195866137622 string  `json:"00195866137622"`
	Num00195866137639 string  `json:"00195866137639"`
	Num00195866137646 string  `json:"00195866137646"`
	Num00195866137653 string  `json:"00195866137653"`
	Num00195866137660 string  `json:"00195866137660"`
	Num00195866137677 string  `json:"00195866137677"`
	Num00195866137684 string  `json:"00195866137684"`
}

然后是我的代码:

data := `{"sku": "BQ4422-400", "image": "https://static.nike.com/a/images/t_default/d099eea8-f876-477a-8333-a84e9acd8584/chaussure-air-jordan-1-high-85.png", "Nom": "Chaussure Air Jordan 1 High '85", "prix": 189.99, "00195866137462": "35.5", "00195866137479": "36", "00195866137486": "36.5", "00195866137493": "37.5", "00195866137509": "38", "00195866137516": "38.5", "00195866137523": "39", "00195866137530": "40", "00195866137547": "40.5", "00195866137554": "41", "00195866137561": "42", "00195866137578": "42.5", "00195866137585": "43", "00195866137592": "44", "00195866137608": "44.5", "00195866137615": "45", "00195866137622": "45.5", "00195866137639": "46", "00195866137646": "47", "00195866137653": "47.5", "00195866137660": "48.5", "00195866137677": "49.5", "00195866137684": "50.5"}`
var shoe LstBQ4422400
json.Unmarshal([]byte(data), &shoe)

问题是,我想以这种方式调用一个键:shoe.aStringIGotusingAList,其中aStringIGotusingAList是一个类似于00195866137684的数字,所以我可以得到50.5。它不允许我放入除声明的键之外的内容。

英文:

lets say i have my structure :

type LstBQ4422400 struct {
	Sku               string  `json:"sku"`
	Image             string  `json:"image"`
	Nom               string  `json:"Nom"`
	Prix              float64 `json:"prix"`
	Num00195866137462 string  `json:"00195866137462"`
	Num00195866137479 string  `json:"00195866137479"`
	Num00195866137486 string  `json:"00195866137486"`
	Num00195866137493 string  `json:"00195866137493"`
	Num00195866137509 string  `json:"00195866137509"`
	Num00195866137516 string  `json:"00195866137516"`
	Num00195866137523 string  `json:"00195866137523"`
	Num00195866137530 string  `json:"00195866137530"`
	Num00195866137547 string  `json:"00195866137547"`
	Num00195866137554 string  `json:"00195866137554"`
	Num00195866137561 string  `json:"00195866137561"`
	Num00195866137578 string  `json:"00195866137578"`
	Num00195866137585 string  `json:"00195866137585"`
	Num00195866137592 string  `json:"00195866137592"`
	Num00195866137608 string  `json:"00195866137608"`
	Num00195866137615 string  `json:"00195866137615"`
	Num00195866137622 string  `json:"00195866137622"`
	Num00195866137639 string  `json:"00195866137639"`
	Num00195866137646 string  `json:"00195866137646"`
	Num00195866137653 string  `json:"00195866137653"`
	Num00195866137660 string  `json:"00195866137660"`
	Num00195866137677 string  `json:"00195866137677"`
	Num00195866137684 string  `json:"00195866137684"`
}

then my code :

data := `{"sku": "BQ4422-400", "image": "https://static.nike.com/a/images/t_default/d099eea8-f876-477a-8333-a84e9acd8584/chaussure-air-jordan-1-high-85.png", "Nom": "Chaussure Air Jordan 1 High '85", "prix": 189.99, "00195866137462": "35.5", "00195866137479": "36", "00195866137486": "36.5", "00195866137493": "37.5", "00195866137509": "38", "00195866137516": "38.5", "00195866137523": "39", "00195866137530": "40", "00195866137547": "40.5", "00195866137554": "41", "00195866137561": "42", "00195866137578": "42.5", "00195866137585": "43", "00195866137592": "44", "00195866137608": "44.5", "00195866137615": "45", "00195866137622": "45.5", "00195866137639": "46", "00195866137646": "47", "00195866137653": "47.5", "00195866137660": "48.5", "00195866137677": "49.5", "00195866137684": "50.5"}`
	var shoe LstBQ4422400	
	json.Unmarshal([]byte(data), &shoe)

the problem is i want to call a key this way : shoe.aStringIGotusingAList ,aStringIGotusingAList refers to a number like 00195866137684, so i get 50.5.
it doesnt let me put another thing than the key declared

答案1

得分: 1

结构体成员(结构体的“字段”)的名称可以任意指定,只要你使用结构体标签指定“json”名称即可。所以,使用你的示例,你可以这样编写结构体...

type LstBQ4422400 struct {
    Sku                   string  `json:"sku"`
    Image                 string  `json:"image"`
    Nom                   string  `json:"Nom"`
    Prix                  float64 `json:"prix"`
    ...
    AStringIGotusingAList string  `json:"00195866137684"`
}

你需要将你想要在stdlib json包中使用的字段大写,因为它只能处理导出的成员(大写的)。

你可以在这里看到一个完整的示例:https://go.dev/play/p/4XsKcdEz6R9

英文:

The names of the members of the struct (the structs "fields") can be anything you want as long as you specify the "json" name using the struct tag. So using your example you could write the struct this way...

type LstBQ4422400 struct {
    Sku                   string  `json:"sku"`
    Image                 string  `json:"image"`
    Nom                   string  `json:"Nom"`
    Prix                  float64 `json:"prix"`
    ...
    AStringIGotusingAList string  `json:"00195866137684"`
}

You will need to capitalize the fields you want to use with the stdlib json package because it only works with exported members (capitalized).

You can see a complete example here: https://go.dev/play/p/4XsKcdEz6R9

huangapple
  • 本文由 发表于 2022年4月4日 22:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/71738949.html
匿名

发表评论

匿名网友

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

确定