如何将 JSON 解组为 dynamodb.AttributeValue?

例如,在输入时:


[

        {

            "M": {

                "Capacity": {

                    "S": "7"

                },

                "Energy": {

                    "S": "A+"

                }

            }

        },

        {

            "M": {

                "Capacity": {

                    "S": "7"

                },

                "Energy": {

                    "S": "A++"

                }

            }

        },

        {

            "M": {

                "Capacity": {

                    "S": "7"

                },

                "Energy": {

                    "S": "A+++"

                }

            }

        }

    ]

而不是Capacity,Energy我可以获得其他密钥。我没有找到任何方法来准备dynamodb.AttributeValue适用于UpdateItem.


我试图解组[]map[string]interface{},然后转换为dynamodb.AttributeValue.


慕丝7291255
浏览 112回答 1
1回答

慕尼黑8549860

unmarshal into []map[string]map[string]*dynamodb.AttributeValue{}thenCapacity和 Energykey 都 unmarshal as dynamodb.AttributeValue。因此,您可以使用外部映射的类型值,map[string]*dynamodb.AttributeValue如ExpressionAttributeValuesUpdateItemInputUpdateItempackage mainimport (    "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)    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go