使用php检索json文档属性

时间:2016-03-30 18:21:39

标签: php json couchbase

我有以下用于查询couchbase数据库的php逻辑:

$myCluster = new CouchbaseCluster('couchbase://localhost');
$bucket = $myCluster->openBucket('reporting');
$result = $bucket->get('server_details_1');
$doc = $result->value;
var_dump($doc);
echo $doc->server_details->name."\n";

它在最后一行失败了因为我相信我尝试访问json对象的方式不正确。也许它不再是一个json对象......我不太确定。但我知道couchbase将它存储为数据库本身的json。

这是var_dump命令返回的内容,它显示了对象的结构:

string '{"server_details":{"name":"test_server","dns_name":"test.server.mydomain.net","ipv4":"10.1.16.106","ipv6":"","type":"primary"},"server_status":{"up_to_date":true},"packages":{"pack1":{"current_version":"x.x","previous_version":"x.x","last_updated_on":"2016-03-16","play_id":"link to audit table"},"pack2":{"current_version":"x.x","previous_version":"x.x","last_updated_on":"2016-03-16","play_id":"link to audit table"}}}' (length=438)

1 个答案:

答案 0 :(得分:1)

您必须首先json_decode您的数据,因为当您从服务器获取数据时,它只是一个字符串。

$doc = json_decode($result->value);
echo $doc->server_details->name."\n";