I had meet A Json Unmarshal Situation
type Item struct {
Price int64 `json:"price"`
Id int64 `json:"id"`
}
import (
json "project/pkg/utils/json"
)
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
2
Answers
Working code:
json.Unmarshal
function to decode the JSON data into a slice of Item structs. The &items argument tojson.Unmarshal
is a pointer to the slice variable items, which allows the json package to modify the slice directly.