如何使用php获取此字符串中的数据?

时间:2017-10-05 08:27:30

标签: php json

我想从此字符串中获取图片网址http://www.example.com/xx.jpghttp://www.example.com/yy.jpg

{
   "attachments": {
      "data": [
         {
            "subattachments": {
               "data": [
                  {
                     "description": "this is description",
                     "media": {
                        "image": {
                           "height": 720,
                           "src": "http://www.example.com/xx.jpg",
                           "width": 405
                        }
                     }
                  },
                  {
                     "media": {
                        "image": {
                           "height": 720,
                           "src": "http://www.example.com/yy.jpg",
                           "width": 701
                        }
                     },
                     "target": {
                        "id": "123456789",
                        "url": "http://www.example.com"
                     },
                     "type": "text",
                     "url": "http://www.example.com"
                  }
               ]
            },
            "target": {
               "id": "123456",
               "url": "http://www.example.com"
            },
            "title": "This is title",
            "type": "number",
            "url": "http://www.example.com"
         }
      ]
   },
   "id": "12345678901234567890"
}

所以我使用这段代码

$jason_data = file_get_contents('http://www.test.com');
$result_jason_data = json_decode($jason_data);
echo $result_jason_data->attachments->data->media[0]->image;
echo $result_jason_data->attachments->data->media[1]->image;

但我没有任何数据,请你告诉我该怎么办?

1 个答案:

答案 0 :(得分:0)

$array = json_decode($json);
$xxImage = $array->attachments->data[0]->subattachments->data[0]->media->image->src;
$yyImage  = $array->attachments->data[0]->subattachments->data[1]->media->image->src;
echo $xxImage;
echo "<br>";
echo $yyImage;