'call json keys undeclared in golang
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | freeformz |
