【问题标题】:Weird JSON structure reading from Azure Table Storage从 Azure 表存储读取奇怪的 JSON 结构
【发布时间】:2018-05-06 09:30:29
【问题描述】:

我正在使用 Azure 函数中的 azure-storagemodule 从我的 Azure 存储表中读取数据。

获取这样的行

var tableSvc = azure.createTableService();
var query = new azure.TableQuery().top(1000);
tableSvc.queryEntities('tablename', query, null, function(error, result, response) {
    // work with result.entries
}

生成的对象看起来很奇怪,因为每个列的值都使用“_”键放入它自己的对象中,因此 JSON 看起来像这样:

{
    "PartitionKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "RowKey": {
        "$": "Edm.String",
        "_": "Keyname"
    },
    "Title": {
        "_": "The Item Title"
    }
}

而不是我所期望的:

{
    "PartitionKey": "Keyname",
    "RowKey": "Keyname",
    "Title": "The Item Title"
}

该表在 Azure 存储资源管理器中看起来很正常。这是正常的吗?或者我能以某种方式影响查询的输出吗?

【问题讨论】:

    标签: node.js azure-storage azure-functions azure-table-storage


    【解决方案1】:

    这是设计使然。在他们的文档中查看此示例:https://docs.microsoft.com/en-us/azure/cosmos-db/table-storage-how-to-use-nodejs#add-an-entity-to-a-table

    这可能是因为他们试图维护一个类型系统,即使在 JS 中也是如此。

    也许写一个方法来为你抽象出来?

    function getFromTable(cb) {
       var tableSvc = azure.createTableService();
       var query = new azure.TableQuery().top(1000);
       tableSvc.queryEntities('tablename', query, null, function(error, result, response) {
           var response = {};
           for(var item in result) {
               if(result.hasOwnProperty(item)) {
                   response[item] = result[item]["_"];
               }
            }
            cb(response);
       }
    }
    

    我也可能会转而使用 Promises 而不是回调,但这是个人选择。

    【讨论】:

      【解决方案2】:

      您可以将负载格式指定为application/json;odata=nometadata,然后通过response.body.value 获取结果对象。

      var options = { payloadFormat: "application/json;odata=nometadata" };
      tableSvc.queryEntities('tablename', query, null, options, function(error, result, response) {
          if(!error) {
              console.log(response.body.value);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 1970-01-01
        • 2016-03-16
        • 2012-12-20
        • 2020-10-23
        • 2015-11-04
        相关资源
        最近更新 更多