0

i want to get image url http://www.example.com/xx.jpg and http://www.example.com/yy.jpg from this string

{
   "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"
}

so i use this code

$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;

But i not have any data , could you please tell me how can i do ?

  • 5
    Possible duplicate of [How to parse JSON](https://stackoverflow.com/questions/2591098/how-to-parse-json) – B001ᛦ Oct 05 '17 at 08:29
  • Yes, this can be done step by step, and check the result in between. I think you've missed an array. – KIKO Software Oct 05 '17 at 08:30
  • `echo $result_jason_data->attachments->data[0]->subattachments->data[0]->media->image->src;` – Jigar Shah Oct 05 '17 at 08:35
  • You can't skip objects/arrays in your JSON, you need to provide the full path. `$result_json_data->attachments->data[0]->subattachments->data[0]->media->image->src` or similar. I've lost track. ;) – DarthJDG Oct 05 '17 at 08:36

1 Answers1

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;
SaltyPotato
  • 192
  • 2
  • 14