【问题标题】:Convert Azure Table storage entities to JSON将 Azure 表存储实体转换为 JSON
【发布时间】:2013-10-14 04:45:48
【问题描述】:

有没有办法将 Azure 表存储实体转换为 JSON?

PHP 中的 Azure Entity 对象有很多元数据。有没有一种清理它的好方法,并返回一个仅包含相关数据的 JSON 对象。

Entity 对象打印类似这样的内容

WindowsAzure\Table\Models\Entity Object
        (
            [_etag:WindowsAzure\Table\Models\Entity:private] => W/"datetime'2013-10-07T04%3A19%3A37.0155205Z'"
            [_properties:WindowsAzure\Table\Models\Entity:private] => Array
                (
                    [PartitionKey] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => 
                            [_value:WindowsAzure\Table\Models\Property:private] => weather
                        )

                    [RowKey] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => 
                            [_value:WindowsAzure\Table\Models\Property:private] => 0d625293-ef40-492b-bf07-d2889597a8f4
                        )

                    [Timestamp] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => Edm.DateTime
                            [_value:WindowsAzure\Table\Models\Property:private] => DateTime Object
                                (
                                    [date] => 2013-10-07 04:19:37
                                    [timezone_type] => 3
                                    [timezone] => UTC
                                )

                        )

                    [type] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => Edm.Int32
                            [_value:WindowsAzure\Table\Models\Property:private] => 1
                        )

                    [city] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => 
                            [_value:WindowsAzure\Table\Models\Property:private] => Pune
                        )


                    [temperature] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => Edm.Int32
                            [_value:WindowsAzure\Table\Models\Property:private] => 34.7
                        )

                    [localTime] => WindowsAzure\Table\Models\Property Object
                        (
                            [_edmType:WindowsAzure\Table\Models\Property:private] => 
                            [_value:WindowsAzure\Table\Models\Property:private] => 2013-10-07T04:19:31.724Z
                        )

                )

        )

我想要一个像这样的干净的 JSON 输出

{
   PartitionKey:"weather",
   RowKey: "0d625293-ef40-492b-bf07-d2889597a8f4",
   Timestamp: "2013-10-07 04:19:37"
   type: 1,
   city: "Pune",
   tempterature: 34.7
   localTime: "2013-10-07T04:19:31.724Z"
}

我已经实现了一个循环来实现这一点。我不确定这是否是最好的方法。

$entities = $result->getEntities();
        $jsonArray = array();

        for ($i = 0; $i < count($entities); $i++) {
            $arr = $entities[$i]->getProperties();
            $tempArr = array();
            foreach ($arr as $key => $value) {
                if(gettype($entities[$i]->getPropertyValue($key)) != 'object'){

                    $tempArr[$key] = (string)$entities[$i]->getPropertyValue($key);                 }
                else
                {

                    $tempArr[$key] = serialize($entities[$i]->getPropertyValue($key));
                }
            }
            array_push($jsonArray, $tempArr);
        }
        echo json_encode($jsonArray);

【问题讨论】:

  • 不确定The Azure Entity object in PHP has a lot of meta data 是什么意思。你可以解释吗?此外,由于 Azure 表存储在设计上是无架构的,因此没有空属性。
  • 更新了问题。
  • 他的意思是 Azure Entity 对象有很多“超出”预期数据的信息。而且那个物体没有很好的记录,以至于一个局外人如果不问一些问题,就无法合理理解它,这会让他在知情的人看来很愚蠢

标签: php json azure azure-table-storage


【解决方案1】:

现在 Windows Azure 表存储服务支持 JSON,您可以更轻松地做到这一点。您可以在此处查看公告:Windows Azure 存储版本 - 引入 CORS、JSON、分钟指标等。

我有一个简单的例子来说明类似的场景:http://www.contentmaster.com/azure/windows-azure-table-storage-json/

该示例展示了如何使用客户端 JavaScript 直接从 Windows Azure 表上传和下载。

【讨论】:

    【解决方案2】:

    我想你已经回答了你自己的问题:)。我会推荐相同的方法,即循环属性数组并从中创建一个 JSON 表示。

    我确实想提一提的是,目前 JSON 在 Windows Azure Tables 中不受原生支持,但它即将推出(在 7 月的 //Build 会议中进行了演示)。发生这种情况时,您可以直接请求表存储以 JSON 格式返回数据,而无需进行此转换练习。在那之前,我认为这将是您需要遵循的方法。

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-24
      • 2013-08-08
      • 1970-01-01
      • 2017-11-24
      相关资源
      最近更新 更多