简体   繁体   English

如何json_encode由Google Calendar API返回的PHP对象

[英]How to json_encode PHP object returned by Google Calendar API

First and foremost - I am not a PHP developer. 首先 - 我不是PHP开发人员。 I concentrate on front-end but am wanting to expand my knowledge/play around with some personal projects to further my understanding. 我专注于前端,但我想扩展我的知识/玩一些个人项目,以进一步了解。 Therefore some of my terminology and fundamental understand of what I'm trying to achieve may be a little ... 'off'. 因此,我的一些术语和基本理解我想要实现的目标可能有点......“关闭”。

I have followed all the steps provided by Google and have managed to get to a point with the Google Calendar API where I can list calendar events using a simple var_dump() 我已按照Google提供的所有步骤操作,并设法使用Google Calendar API,我可以使用简单的var_dump()列出日历事件

My problem lies in trying to make this data useful. 我的问题在于尝试使这些数据有用。 I want to do this with JSON. 我想用JSON做到这一点。 I am aware of the json_encode() function but am not sure how I should be using it within the context of what I am trying to achieve (that is to have a JSON file which can hook into the data I need to ultimately display a calendar on the front-end). 我知道json_encode()函数,但不知道我应该如何在我想要实现的内容中使用它(即有一个JSON文件可以挂钩到我最终需要显示日历所需的数据在前端)。 I am aware that json_encode() respects property visibility/scope but this is my problem - I cannot figure out how to ensure json_encode() will also encode the protected properties/methods in the object I am returned. 我知道json_encode()尊重属性可见性/范围,但这是我的问题 - 我无法弄清楚如何确保json_encode()还将对我返回的对象中的受保护属性/方法进行编码。

Here is my code: 这是我的代码:

$eventsArgs = array(
    'maxResults' => 10,
    'singleEvents' => true,
);

$events = $calendarService->events->listEvents('***@gmail.com', $eventsArgs);

$listEvents = fopen('list_events.json', 'w');

    foreach($events->getItems() as $event) {
        fwrite($listEvents, json_encode($event, JSON_PRETTY_PRINT));
    }

fclose($listEvents);

I am wanting to pull in protected properties/methods such as start and end as displayed here with a simple var_dump() ... 我想引入受保护的属性/方法,如startend ,如此处显示的简单var_dump() ...

["modelData":protected]=> array(5) {
["creator"]=>
array(3) {
  ["email"]=>
  string(23) "***@gmail.com"
  ["displayName"]=>
  string(14) "***"
  ["self"]=>
  bool(true)
}
["organizer"]=>
array(3) {
  ["email"]=>
  string(23) "***@gmail.com"
  ["displayName"]=>
  string(14) "***"
  ["self"]=>
  bool(true)
}
["start"]=>
array(1) {
  ["dateTime"]=>
  string(25) "2014-04-27T05:00:00+01:00"
}
["end"]=>
array(1) {
  ["dateTime"]=>
  string(25) "2014-04-27T06:00:00+01:00"
}

I am really trying to educate myself here and unfortunately have hit a wall and the answers I find, if I'm honest, are a little too technical for my understanding. 我真的很想在这里教育自己,不幸的是,我找到了答案,如果我诚实的话,我的答案对我的理解来说有点太技术性了。 Technical as well as non-technical explanations welcome! 技术和非技术解释欢迎!

Thanks for reading. 谢谢阅读。

So, the problem, as you pointed out, is that properties you want to access are protected and json_encode respects that and will not access them. 因此,正如您所指出的那样,问题是您要访问的属性受到保护, json_encode尊重它并且不会访问它们。 Read up on public, private, and protected. 阅读公共,私人和受保护的内容。 Good stuff 好东西

Because the values are protected, you'll have to pull the data you want out of $event and into an unprotected array and then json_encode that. 因为这些值是受保护的,所以你必须将你想要的数据从$event提取到一个不受保护的数组中,然后json_encode Untested code follows: 未经测试的代码如下:

$eventsArgs = array(
    'maxResults' => 10,
    'singleEvents' => true,
);

$events = $calendarService->events->listEvents('***@gmail.com', $eventsArgs);

$listEvents = fopen('list_events.json', 'w');

foreach($events->getItems() as $event) {
    $jsonArr = array(
        "start"=>$event->start->dateTime
        ,"end"=>$event->end->dateTime
    );

    fwrite($listEvents, json_encode($jsonArr, JSON_PRETTY_PRINT));
}

fclose($listEvents);

Now even that code might not work. 现在即使该代码也可能无效。 You may have to find the appropriate method (that's 'method' as in: function belonging to a class) to get the properties you want out of a Google Calendar Event, and then put them in an array and than json_encode it. 您可能必须找到适当的方法(即'方法',如:属于某个类的函数),以从Google日历事件中获取您想要的属性,然后将它们放在一个数组中,而不是json_encode它。

So the 'getter' method might be: $event->get('start')->dateTime or something. 所以'getter'方法可能是: $event->get('start')->dateTime或者其他东西。 I have no experience with the Google Calendar API, so can't really help you in that regard 我没有使用Google Calendar API的经验,所以在这方面无法真正帮助您

Lastly, know that you can have the PHP script echo the json_encode d results so you won't need the file. 最后,要知道您可以让PHP脚本echojson_encode d结果,这样您就不需要该文件了。 (of course, I have no idea what all of your code looks like, so that could be a bad idea) (当然,我不知道你的所有代码是什么样的,所以这可能是一个坏主意)

foreach(...) {
    echo json_encode($yourArray);
}

I had the exact same problem as you! 我遇到了与你完全相同的问题! I finally found: 我终于找到了:

$json = json_encode( (array)$events);

Answer is from another post: Here 答案来自另一篇文章: 这里

Please Note: 请注意:


It produces Unicode NUL characters: \*\ on the outer array keys. 它在外部数组键上生成Unicode NUL字符:\\ u0000 * \\ u0000。 But the full structure with the start and end values remains. 但是具有起始值和结束值的完整结构仍然存在。 If you are interested in removing the Nul chars you can use str_replace() or explode. 如果您有兴趣删除Nul字符,可以使用str_replace()或explode。 Understanding what \ is in PHP / JSON and getting rid of it 了解什么是\\ u0000在PHP / JSON中并摆脱它

Terminus's answer above is great but using this was preferable for my situation because I needed to match a different json file from google node.js api . Terminus上面的答案很棒但是使用它对我的情况更好,因为我需要匹配来自google node.js api的不同json文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM