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

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

Golang Json Unmarshal string/int64 into int64

问题

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

import (
    json "project/pkg/utils/json"
)

type Item struct {
    Id    int64 `json:"id"`
    Price int64 `json:"price"`
}

func HandleJsonData() {
    jsonData := []byte(`[{"id":"1000","price":"30"},{"id":"1001","price":50}]`)
    var item []Item
    err := json.Unmarshal(jsonData, &item)
    if err != nil {
        fmt.Println(err)
    }
}

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

英文:

I had meet A Json Unmarshal Situation

import (
json "project/pkg/utils/json"
)

type Item struct {
   Id    int64 `json:"id"`
   Price int64 `json:"price"`
}



func HandleJsonData() {

   jsonData := []byte(`[{"id":"1000,"price":"30"},{"id":"1001,"price":50}]`)

   var item []Item
   err = json.Unmarshal(jsonData, &item)
   if err != nil {
      fmt.Println(err)
   }

}

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

答案1

得分: 1

type Item struct {
   Price json.Number `json:"price"`
   Id    int64 `json:"id"`
}

func HandleJsonData() {

   jsonData := []byte(`[{"id":"1000","price":"30"},{"id":"1001","price":50}]`)

   var item Item
   err = json.Unmarshal(jsonData, &item)
   if err != nil {
      fmt.Println(err)
   }

   price, _ := strconv.ParseInt(string(item.Price), 10, 64)
}
英文:
type Item struct {
   Price json.Number `json:"price"`
   Id    int64 `json:"id"`
}

func HandleJsonData() {

   jsonData := []byte(`[{"id":"1000,"price":"30"},{"id":"1001,"price":50}]`)

   var item Item
   err = json.Unmarshal(jsonData, &item)
   if err != nil {
      fmt.Println(err)
   }

   price, _ := strconv.ParseInt(string(item.Price), 10, 64)
}

答案2

得分: -1

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

type Item struct {
	Price int64 `json:"price"`
	Id    int64 `json:"id"`
}

func TestHandleJsonData(t *testing.T) {

	jsonData := []byte(`[{"id":1000,"price":30},{"id":1001,"price":50}]`)

	var item []Item

	err := json.Unmarshal(jsonData, &item)
	require.NoError(t, err)

	require.NotEmpty(t, item)
	require.Len(t, item, 2)

	require.Equal(t, int64(1000), item[0].Id)
	require.Equal(t, int64(30), item[0].Price)

	require.Equal(t, int64(1001), item[1].Id)
	require.Equal(t, int64(50), item[1].Price)
}

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

英文:

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

type Item struct {
	Price int64 `json:"price"`
	Id    int64 `json:"id"`
}

func TestHandleJsonData(t *testing.T) {

	jsonData := []byte(`[{"id":1000,"price":30},{"id":1001,"price":50}]`)

	var item []Item

	err := json.Unmarshal(jsonData, &item)
	require.NoError(t, err)

	require.NotEmpty(t, item)
	require.Len(t, item, 2)

	require.Equal(t, int64(1000), item[0].Id)
	require.Equal(t, int64(30), item[0].Price)

	require.Equal(t, int64(1001), item[1].Id)
	require.Equal(t, int64(50), item[1].Price)
}

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:

确定