【问题标题】:The JSON Document must be validJSON 文档必须有效
【发布时间】:2020-11-04 22:06:59
【问题描述】:

我正在尝试使用 AWS PHP 开发工具包对 dynamo 中的表进行简单更新。 I get the error The JSON Document must be valid and be an object at its root. 在下面的这段代码上。对我来说,这看起来像是一个有效的 JSON。错误显示在$eav 变量的行上。

                $eav = $marshaler->marshalJson('
                      {":Aname": {"S": ' . $profile['Aname'] . '}}
                 ');

                // dynamodb table update (NEW ATTRIBUTE)
                $params = [
                        'TableName' => 'table-example',
                        'Key' => 'table-key-example',
                        'UpdateExpression' => 
                            'set Aname = :Aname',
                        'ExpressionAttributeValues' => $eav,
                        'ReturnValues' => 'UPDATED_NEW'
                ];

                try {
                        $result = $dynamodb->updateItem($params);
                        echo "Updated item.\n";
                        print_r($result['Attributes']);
                    
                    } catch (DynamoDbException $e) {
                        echo "Unable to update item:\n";
                        echo $e->getMessage() . "\n";
                    }

【问题讨论】:

  • 为什么要手动制作 JSON? $profile['Aname'] 包含什么?
  • ExpressionAttributeValues参数的值不应该是字符串,而是关联数组。 SDK 将为您编组它。因此,不要让您的 $eav 变量成为字符串,只需使用 [":Aname" => ["S" => $profile['Aname']]]
  • 我正在关注文档,这就是创建变量的方式。 $profile['Aname'] 是一个字符串值
  • 您指的是哪个文档?我在看these ones
  • 我在看这些docs

标签: php amazon-web-services sdk aws-sdk


【解决方案1】:
$eav = $marshaler->marshalJson('{":Aname": "' . $profile['Aname'] . '"}');

// dynamodb table update (NEW ATTRIBUTE)
$params = [
    'TableName' => 'table-example',
    'Key' => 'table-key-example',
    'UpdateExpression' => 'set Aname = :Aname',
    'ExpressionAttributeValues' => $eav,
    'ReturnValues' => 'UPDATED_NEW'
];

try {
    $result = $dynamodb->updateItem($params);
    echo "Updated item.\n";
    print_r($result['Attributes']);

} catch (DynamoDbException $e) {
    echo "Unable to update item:\n";
    echo $e->getMessage() . "\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-17
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多