douhu7807 2013-05-12 00:55
浏览 36
已采纳

无法在Google Plus JSON中访问附件数据

I am accessing the Google Plus API and this is one of the results that are returned:

{
 "nextPageToken": "Ci8I0bJLEie2uAPcvrABl96xAdaVqgO93LADn_6ABO2vgwXqpbAFoNmEBsHCpwgYAgovCJXLThInn-8054qFArGHmQKtp-gD_r6-BOqlsAW-7uAGjO7QB4HbngqEoKkLGAEQj5e7jAUY8sK7jAUiAA",
 "items": [
  {
   "id": "z13rwjqhyqzve5tgq04cdxfyfzqtgfszfcw",
   "url": "https://plus.google.com/104261567553968048744/posts/SDs5inGSpEi",
   "actor": {
    "displayName": "Evan Parker",
    "url": "https://plus.google.com/104261567553968048744"
   },
   "verb": "post",
   "object": {
    "attachments": [
     {
      "objectType": "photo",
      "displayName": "#throughglass",
      "id": "104261567553968048744.5876882440165641794",
      "content": "s_00d2c678-b686-49db-9666-a55292cbe59b-0",
      "url": "https://plus.google.com/photos/104261567553968048744/albums/5876882440813674497/5876882440165641794?authkey=CM3iyKyWoa7gcA",
      "image": {
       "url": "https://lh5.googleusercontent.com/-aI28I0V0geM/UY7hcTjNzkI/AAAAAAABzYw/8MxNghGljew/w497-h373/s_00d2c678-b686-49db-9666-a55292cbe59b-0",
       "type": "image/jpeg",
       "height": 373,
       "width": 497
      },
      "fullImage": {
       "url": "https://lh5.googleusercontent.com/-aI28I0V0geM/UY7hcTjNzkI/AAAAAAABzYw/8MxNghGljew/s0-d/s_00d2c678-b686-49db-9666-a55292cbe59b-0",
       "type": "image/jpeg",
       "height": 1888,
       "width": 2560
      }
     }
    ]
   }
  },
  {
   "id": "z12zh1gopsqyep5ox04cef0rwqrtsl1ai54",
   "url": "https://plus.google.com/117367546931116283373/posts/THnmvu6e9FT",
   "actor": {
    "displayName": "Max Braun",
    "url": "https://plus.google.com/117367546931116283373"
   },
   "verb": "post",
   "object": {
    "attachments": [
     {
      "objectType": "photo",
      "displayName": "#throughglass, thanks @mattywyattmartin!",
      "id": "117367546931116283373.5876858377843226994",
      "content": "IMG_20130511_153648.jpg",
      "url": "https://plus.google.com/photos/117367546931116283373/albums/5876858374868052977/5876858377843226994?authkey=CPT71f-zl_iToQE",
      "image": {
       "url": "https://lh4.googleusercontent.com/-E_OT7cr5qj4/UY7LjsafuXI/AAAAAAAA1yg/RAEswDK1-iw/w497-h373/IMG_20130511_153648.jpg",
       "type": "image/jpeg",
       "height": 373,
       "width": 497
      },
      "fullImage": {
       "url": "https://lh4.googleusercontent.com/-E_OT7cr5qj4/UY7LjsafuXI/AAAAAAAA1yg/RAEswDK1-iw/s0-d/IMG_20130511_153648.jpg",
       "type": "image/jpeg",
       "height": 1276,
       "width": 1276
      }
     }
    ]
   }
  }
 ]
}

This is how I am accessing it with PHP:

$posts = json_decode($content);
foreach ($posts->items as $value){
    echo "<b>ID: </b>" . $id . "<br />";
    echo "<b>Name: </b>" . $value->actor->displayName . "<br />";
    echo "<b>Profile URL: </b>" . $value->actor->url . "<br />";
    echo "<b>Post URL: </b>" . $value->url . "<br />";
    echo "<b>Post Text: </b>" . $value->object->attachments->displayName . "<br />";
    echo "<b>Image Small: </b>" . $value->object->attachments->image->url . "<br />";
    echo "<b>Image Full: </b>" . $value->object->attachments->fullImage->url . "<br /><br />";
    $count++;
}

For some reason I cannot get the attachment data because it is nested under "object". Is there a fix or a workaround that will work for this?

  • 写回答

1条回答 默认 最新

  • duaeim2874 2013-05-12 01:02
    关注
    echo "<b>Post Text: </b>" . $value->object->attachments[0]->displayName . "<br />";
    echo "<b>Image Small: </b>" . $value->object->attachments[0]->image->url . "<br />";
    echo "<b>Image Full: </b>" . $value->object->attachments[0]->fullImage->url . "<br /><br />";
    

    The attachments portion is an array. So you need to access it via it's array index then you can access the properties of the object in the array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)