例如,在输入时:
[
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A+"
}
}
},
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A++"
}
}
},
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A+++"
}
}
}
]
而不是
Capacity
, Energy
我可以得到其他 key 。我没有找到任何方法可以从此类输入中进行准备 dynamodb.AttributeValue
适用于UpdateItem
.我试图解码为
[]map[string]interface{}
然后转换为 dynamodb.AttributeValue
.
最佳答案
解码到 []map[string]map[string]*dynamodb.AttributeValue{}
然后 Capacity
和 Energy
key 解码为 dynamodb.AttributeValue
.
因此,您可以使用 map[string]*dynamodb.AttributeValue
类型的外部映射值如ExpressionAttributeValues
在 UpdateItemInput
至UpdateItem
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/service/dynamodb"
)
func main() {
data := []byte(`[
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A+"
}
}
},
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A++"
}
}
},
{
"M": {
"Capacity": {
"S": "7"
},
"Energy": {
"S": "A+++"
}
}
}
]`)
d := []map[string]map[string]*dynamodb.AttributeValue{}
json.Unmarshal(data, &d)
for _, mp := range d {
expressionAttributeValues := mp["M"]
// you can use expressionAttributeValues as ExpressionAttributeValues in UpdateItemInput to UpdateItem
capacity := expressionAttributeValues["Capacity"]
energy := expressionAttributeValues["Energy"]
fmt.Printf("%+v", capacity)
fmt.Printf("%+v", energy)
}
}
关于go - 如何将 JSON 解码为 dynamodb.AttributeValue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61092035/