0

是否有任何简单的方法可以使用AWS SDK CPP将项目放入Amazon DynamoDB中,并使用JSON格式的项目作为输入?就像是

Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
request.FunctionThatSetsAllAttributesParsingAJsonString(json_string);

还是总是需要设置每个属性及其类型?

Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
Aws::DynamoDB::Model::AttributeValue val1;
val1.SetS(str);
request.AddItem(key, val1);
Aws::DynamoDB::Model::AttributeValue val2;
...
4

1 回答 1

0

如果JSON对象遵循Amazon DynamoDB JSON格式,您可以轻松地将其转换为Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>对象。例如,对于Jsoncpp Json::Value对象:

Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> json2atts(const Json::Value& json) {
    try {
        Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue> amap;
        Aws::Utils::Json::JsonValue jval(Json::FastWriter().write(json));
        if (!jval.WasParseSuccessful()) {
            throw exception("Failed to parse input JSON");
        };
        Aws::Utils::Json::JsonView jview = jval.View();
        Aws::Map<Aws::String, Aws::Utils::Json::JsonView> jmap = jview.GetAllObjects();
        for(auto& i : jmap) {
            amap[i.first] = i.second.AsObject();
        };
        return amap;
    }
    catch (std::exception &e) { 
       ...
    };
};

然后,放置Aws::Map<Aws::String, Aws::DynamoDB::Model::AttributeValue>对象:

Aws::DynamoDB::Model::PutItemRequest request;
request.SetTableName(table);
request.SetItem(amap);
于 2021-04-23T08:12:29.797 回答