【问题标题】:How to read array Result provided by google drive api [duplicate]如何读取google drive api提供的数组结果[重复]
【发布时间】:2016-07-13 06:59:59
【问题描述】:

我正在尝试读取 google drive api 提供的 json,但无法读取 json。

我正在尝试读取模型数据:受保护的数据。

我在 $result 下得到以下结果

[0] => Google_Service_Drive_DriveFile Object (
    [modelData:protected] => Array
            (
                [labels] => Array
                    (
                        [starred] => 
                        [hidden] => 
                        [trashed] => 
                        [restricted] => 
                        [viewed] => 
                    )

                [parents] => Array
                    (
                        [0] => Array
                            (
                                [kind] => drive#parentReference
                                [id] => 0B4tddddcc03RW42UTdjUlY3SDg
                                [selfLink] => 
                                [parentLink] =>
                                [isRoot] => 
                            )

                    )

         )



)

我正在尝试阅读父母[0]

我写了这段代码

foreach ($result as $res) {

print_r($res[modelData:protected]); 
}

这给了我错误

解析错误:语法错误,意外':',期待']'在

有什么想法吗?如何解决这个问题

【问题讨论】:

  • 试试print_r($res["modelData:protected"]); 。它需要数组键中的引号。
  • @M4R1KU:我有 modelData:protected 并且您提供的解决方案不起作用.. 空白屏幕
  • 在我看来,您无法以通常的方式获得该对象的保护。
  • @Arun:我只有空白屏幕
  • @rad11:那么有什么替代解决方案吗?

标签: php json


【解决方案1】:

您发布的$result 对象似乎是属于 Google PHP 库的 Google_Service_Drive_DriveFile 对象。

OOP visibility::protected 规则规定,您不能在课堂外访问受保护的属性(通过谷歌搜索了解有关 OOP Visibility 概念的更多信息)。

如果你想列出与类相关的所有函数,你可以使用 php get_class_methods()

print_R(get_class_methods($result));
# it will all methods available to this class.

或者您可以在库代码库中搜索适合您要求的所需方法。

根据 Libray 使用的编码标准,它为每个 私有或受保护 属性使用 getter 和 setter;

# for example if you want to access labels from above code then you can use 
$array = $result->getLabels();

# to get Parents
$parents = $result->getParents();

# if you want to set this properties you can use
$result->setLabels($array);

【讨论】:

  • 你能告诉我如何使用api读取文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-27
  • 1970-01-01
相关资源
最近更新 更多