gpt4 book ai didi

php - 将 Azure 表存储实体转换为 JSON

转载 作者:行者123 更新时间:2023-12-04 04:38:51 25 4
gpt4 key购买 nike

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

PHP 中的 Azure 实体对象拥有大量元数据。有没有好的方法来清理它,并返回一个仅包含相关数据的 JSON 对象。

实体对象打印类似这样的内容

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);

最佳答案

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

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

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

关于php - 将 Azure 表存储实体转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19227753/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com