Golang中将字符串/ int64解析为int64的方法是使用json.Unmarshal函数。

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

Golang Json Unmarshal string/int64 into int64

问题

我遇到了一个Json解组的情况

  1. import (
  2. json "project/pkg/utils/json"
  3. )
  4. type Item struct {
  5. Id int64 `json:"id"`
  6. Price int64 `json:"price"`
  7. }
  8. func HandleJsonData() {
  9. jsonData := []byte(`[{"id":"1000","price":"30"},{"id":"1001","price":50}]`)
  10. var item []Item
  11. err := json.Unmarshal(jsonData, &item)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. }

我无法正确地解组json数据。如何解决这个问题?

英文:

I had meet A Json Unmarshal Situation

  1. import (
  2. json "project/pkg/utils/json"
  3. )
  4. type Item struct {
  5. Id int64 `json:"id"`
  6. Price int64 `json:"price"`
  7. }
  8. func HandleJsonData() {
  9. jsonData := []byte(`[{"id":"1000,"price":"30"},{"id":"1001,"price":50}]`)
  10. var item []Item
  11. err = json.Unmarshal(jsonData, &item)
  12. if err != nil {
  13. fmt.Println(err)
  14. }
  15. }

I cannot direct urmarshal jsondata correctly.
How to solve this situation

答案1

得分: 1

  1. type Item struct {
  2. Price json.Number `json:"price"`
  3. Id int64 `json:"id"`
  4. }
  5. func HandleJsonData() {
  6. jsonData := []byte(`[{"id":"1000","price":"30"},{"id":"1001","price":50}]`)
  7. var item Item
  8. err = json.Unmarshal(jsonData, &item)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. price, _ := strconv.ParseInt(string(item.Price), 10, 64)
  13. }
英文:
  1. type Item struct {
  2. Price json.Number `json:"price"`
  3. Id int64 `json:"id"`
  4. }
  5. func HandleJsonData() {
  6. jsonData := []byte(`[{"id":"1000,"price":"30"},{"id":"1001,"price":50}]`)
  7. var item Item
  8. err = json.Unmarshal(jsonData, &item)
  9. if err != nil {
  10. fmt.Println(err)
  11. }
  12. price, _ := strconv.ParseInt(string(item.Price), 10, 64)
  13. }

答案2

得分: -1

以下是你提供的代码的翻译:

  1. type Item struct {
  2. Price int64 `json:"price"`
  3. Id int64 `json:"id"`
  4. }
  5. func TestHandleJsonData(t *testing.T) {
  6. jsonData := []byte(`[{"id":1000,"price":30},{"id":1001,"price":50}]`)
  7. var item []Item
  8. err := json.Unmarshal(jsonData, &item)
  9. require.NoError(t, err)
  10. require.NotEmpty(t, item)
  11. require.Len(t, item, 2)
  12. require.Equal(t, int64(1000), item[0].Id)
  13. require.Equal(t, int64(30), item[0].Price)
  14. require.Equal(t, int64(1001), item[1].Id)
  15. require.Equal(t, int64(50), item[1].Price)
  16. }

希望对你有帮助!如果你有任何其他问题,请随时提问。

英文:

everything works as it should, just fix the errors in the line you are parsing (look in my code)

  1. type Item struct {
  2. Price int64 `json:"price"`
  3. Id int64 `json:"id"`
  4. }
  5. func TestHandleJsonData(t *testing.T) {
  6. jsonData := []byte(`[{"id":1000,"price":30},{"id":1001,"price":50}]`)
  7. var item []Item
  8. err := json.Unmarshal(jsonData, &item)
  9. require.NoError(t, err)
  10. require.NotEmpty(t, item)
  11. require.Len(t, item, 2)
  12. require.Equal(t, int64(1000), item[0].Id)
  13. require.Equal(t, int64(30), item[0].Price)
  14. require.Equal(t, int64(1001), item[1].Id)
  15. require.Equal(t, int64(50), item[1].Price)
  16. }

huangapple
  • 本文由 发表于 2023年7月14日 19:45:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76687365.html
匿名

发表评论

匿名网友

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

确定