使用 slice returns 空值而不是空 slice 解组结构
Unmarshal to struct with slice returns null value instead of empty slice
如果我创建一个 "photo" 没有任何标签,它存储在 dynamodb 中为
"tags": {
"NULL": true
},
但是当我查询和解组记录时,我希望它会将其转换为一个空切片,而不是我得到这个:
[{"photo_id":"bmpuh3jg","tags":null}]
是否可以将其转换为空切片?例如
[{"photo_id":"bmpuh3jg","tags":[]}]
代码示例
我的结构
type Photo struct {
Id string `json:"photo_id"`
Tags []string `json:"tags"`
}
查询
photo := &Photo{}
input := &dynamodb.QueryInput{
TableName: aws.String("local.photos"),
KeyConditionExpression: aws.String("photo_id = :photo_id"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":photo_id": {
S: aws.String(photo_id),
},
},
}
db_result, err := db.Query(input)
if err != nil {
return nil, err
} else if *db_result.Count == int64(0) {
// No item found
return nil, err
}
err = dynamodbattribute.UnmarshalListOfMaps(db_result.Items, photo)
if err != nil {
return nil, err
}
photoJSON, err := json.Marshal(photo)
if err != nil {
return nil, err
}
return photoJSON, nil
如果我对你的问题的理解正确,要获得标签 ({"photo_id":"bmpuh3jg","tags":[]}
) 的空切片的结果,你可以这样做:
jsonString := `{"photo_id":"bmpuh3jg","tags":null}`
photo := &Photo{}
err := json.Unmarshal([]byte(jsonString), &photo)
if err != nil {
fmt.Println(err.Error())
}
// Here is a trick. Replace nil with an empty slice.
if photo.Tags == nil {
photo.Tags = []string{}
}
elemJSON, err := json.Marshal(photo)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(elemJSON)) //{"photo_id":"bmpuh3jg","tags":[]}
要理解为什么 nil 切片编码为 null JSON,您可以查看官方文档 https://golang.org/pkg/encoding/json/
Array and slice values encode as JSON arrays, except that []byte
encodes as a base64-encoded string, and a nil slice encodes as the
null JSON value.
查看 Go Playground:https://play.golang.org/p/BsxTpBlypV5
如果我创建一个 "photo" 没有任何标签,它存储在 dynamodb 中为
"tags": {
"NULL": true
},
但是当我查询和解组记录时,我希望它会将其转换为一个空切片,而不是我得到这个:
[{"photo_id":"bmpuh3jg","tags":null}]
是否可以将其转换为空切片?例如
[{"photo_id":"bmpuh3jg","tags":[]}]
代码示例
我的结构
type Photo struct {
Id string `json:"photo_id"`
Tags []string `json:"tags"`
}
查询
photo := &Photo{}
input := &dynamodb.QueryInput{
TableName: aws.String("local.photos"),
KeyConditionExpression: aws.String("photo_id = :photo_id"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":photo_id": {
S: aws.String(photo_id),
},
},
}
db_result, err := db.Query(input)
if err != nil {
return nil, err
} else if *db_result.Count == int64(0) {
// No item found
return nil, err
}
err = dynamodbattribute.UnmarshalListOfMaps(db_result.Items, photo)
if err != nil {
return nil, err
}
photoJSON, err := json.Marshal(photo)
if err != nil {
return nil, err
}
return photoJSON, nil
如果我对你的问题的理解正确,要获得标签 ({"photo_id":"bmpuh3jg","tags":[]}
) 的空切片的结果,你可以这样做:
jsonString := `{"photo_id":"bmpuh3jg","tags":null}`
photo := &Photo{}
err := json.Unmarshal([]byte(jsonString), &photo)
if err != nil {
fmt.Println(err.Error())
}
// Here is a trick. Replace nil with an empty slice.
if photo.Tags == nil {
photo.Tags = []string{}
}
elemJSON, err := json.Marshal(photo)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(string(elemJSON)) //{"photo_id":"bmpuh3jg","tags":[]}
要理解为什么 nil 切片编码为 null JSON,您可以查看官方文档 https://golang.org/pkg/encoding/json/
Array and slice values encode as JSON arrays, except that []byte encodes as a base64-encoded string, and a nil slice encodes as the null JSON value.
查看 Go Playground:https://play.golang.org/p/BsxTpBlypV5