-1

I have a following JSON File. I have tried accessing it; but, I am getting undefined error. Please suggest how to read it using PHP:

"feed": {
   "entry": [
   {
    "id": "679244143963",
    "title": "Nashware Black Travel Kit",
    "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
    "link": "http://www.sl.com/product/travel-kit/679244143963",
    "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
    "sub_category_id": "435",
    "sub_category_name": "Travel Accessories",
    "mrp": "700",
    "availability": "in stock",
    "effective_price": "310"
  },
   {
    "id": "679244143963",
    "title": "Nashware Black Travel Kit",
    "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
    "link": "http://www.sl.com/product/travel-kit/679244143963",
    "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
    "sub_category_id": "435",
    "sub_category_name": "Travel Accessories",
    "mrp": "700",
    "availability": "in stock",
    "effective_price": "310"
  }
   ]
  }

After Entry same as JSON notation Id, title, desc and etc are displayed in array. Any suggestions.

Neil
  • 14,063
  • 3
  • 30
  • 51
Dhruvi Mistry
  • 122
  • 1
  • 3
  • 13

2 Answers2

0

You have missed start and end brackets. Your json should look like:

    {
    "feed": {
        "entry": [{
            "id": "679244143963",
            "title": "Nashware Black Travel Kit",
            "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
            "link": "http://www.sl.com/product/travel-kit/679244143963",
            "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
            "sub_category_id": "435",
            "sub_category_name": "Travel Accessories",
            "mrp": "700",
            "availability": "in stock",
            "effective_price": "310"
        },
        {
            "id": "679244143963",
            "title": "Nashware Black Travel Kit",
            "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
            "link": "http://www.sl.com/product/travel-kit/679244143963",
            "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
            "sub_category_id": "435",
            "sub_category_name": "Travel Accessories",
            "mrp": "700",
            "availability": "in stock",
            "effective_price": "310"
        }]
    }
}

Or you have to use below code:

$obj = json_decode("{" . $json . "}");
VirCom
  • 3,414
  • 1
  • 10
  • 10
  • More likely the `feed` key is part of the APIs response structure and should be removed. – arkascha Apr 06 '17 at 08:37
  • Hi, Vircom , I converted xml file into Json , and the code I have written is the one I got from the online converter , I don't know much. Can you please share how to access it. – Dhruvi Mistry Apr 06 '17 at 08:38
  • @arkascha , sure will do that , As I don't know where to start I simply paste that here. Do help if you can in accessing it. – Dhruvi Mistry Apr 06 '17 at 08:40
  • JSON content, that you pasted is valid. But php require additional brackets. Could you paste part of your code that not working correct ? – VirCom Apr 06 '17 at 08:41
  • I have tried this 2 options : foreach($result as $re){ echo $re.feed.entry["title"]; // echo $re[feed]->[entry]{title}; } – Dhruvi Mistry Apr 06 '17 at 08:43
  • So how about: foreach($obj->feed->entry as $entry) { echo $entry->title; } – VirCom Apr 06 '17 at 08:48
0

It seems your JSON is structured as follows :

{"feed" : {
   "entry" : [array of JSON objects]
 }
}

So after doing some research, it seems the best way to do it would be to use RecursiveArrayIterator. I found the answer on another post, here.

 <?php

$json = <<< JSON
{
 "feed": {
        "entry": [{
            "id": "679244143963",
            "title": "Nashware Black Travel Kit",
            "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
            "link": "http://www.sl.com/product/travel-kit/679244143963",
            "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
            "sub_category_id": "435",
            "sub_category_name": "Travel Accessories",
            "mrp": "700",
            "availability": "in stock",
            "effective_price": "310"
        },
        {
            "id": "679244143963",
            "title": "Nashware Black Travel Kit",
            "description": "Color : Black Material : Others Type : Travel kits Closure : Magnit Button Combo : No Compartment : 4 Dimension (LxHxW) cm : 25X51X7 Disclaimer : Product colour may slightly vary due to photographic lighting sources or your monitor settings Gender : Women ",
            "link": "http://www.sl.com/product/travel-kit/679244143963",
            "image_link": "http://n1.sdlcdn.com/imgs/b/e/t/MU_COS_BAG_M_1_3x-0b20f.jpg",
            "sub_category_id": "435",
            "sub_category_name": "Travel Accessories",
            "mrp": "700",
            "availability": "in stock",
            "effective_price": "310"
        }]
    }
}
JSON;

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:\n";
    } else {
        echo "$key => $val\n";
    }
}

Here you can run the code one codepad

Community
  • 1
  • 1
Xogno
  • 112
  • 2
  • 12