清理DynamoDB的JSON输出

时间:2018-03-28 05:45:23

标签: json amazon-web-services amazon-dynamodb

我有来自AWS文档的这个脚本:

<script>
  AWS.config.update({
    region: "us-east-2",
    endpoint: 'https://dynamodb.us-east-2.amazonaws.com',
    // accessKeyId default can be used while using the downloadable version of DynamoDB. 
    // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    accessKeyId: "myfakeid",
    // secretAccessKey default can be used while using the downloadable version of DynamoDB. 
    // For security reasons, do not store AWS Credentials in your files. Use Amazon Cognito instead.
    secretAccessKey: "myfakekey"
  });

  var docClient = new AWS.DynamoDB.DocumentClient();

  function readItem() {
    var table = "CONNECT_STATUS";
    var CGW_ID = "FF00000001";
    var CON_ID = "EE00000001";

    var params = {
      TableName: "MyTable",
      Key: {
        "CGW_ID": CGW_ID,
        "CON_ID": CON_ID
      },
      "ProjectionExpression": "Curr"
    };

    docClient.get(params, function(err, data) {
      if (err) {
        document.getElementById('textarea').innerHTML = "Unable to read item: " + "\n" + JSON.stringify(err, undefined, 2);
      } else {
        document.getElementById('textarea').innerHTML = JSON.stringify(data, undefined, 2) + " Amps";
      }
    });
  }
</script>

以下HTML:

<input class="refresh "id="readItem" type="image" src="img/refresh.png" onclick="readItem();" />
<br><br>
<textarea readonly id="textarea" style="width: 150px; height: 50px"></textarea>

我遇到的麻烦是,当输出返回时,它以JSON格式返回,带有大括号和单词&#34; Item&#34;。是否可以仅提取名为&#34; Curr&#34;的列的值。没有任何大括号或文字或引号?只有价值。

此外,是否可以对&#34; Curr&#34;的值进行计算?无论是在前端还是需要其他东西?

我是DynamoDB的新手并且正在学习。非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

你应该可以这样做:

document.getElementById("textarea").innerHTML = data.Item.Curr + " Amps";

也:

// Convert the `Curr` numeric string to a number.
var curr = Number(data.Item.Curr);