json_decode and arrays

MultiformeIngegno

  • Posts: 1,337
Re: json_decode and arrays
« Reply #46, on June 14th, 2013, 01:23 PM »Last edited on June 14th, 2013, 02:11 PM
This is the tree of my JSON (stored in $data_delicious):

Code: [Select]
{
   "@attributes":{
      "tag":"",
      "user":"myusername"
   },
   "post":[
      {
         "@attributes":{
            "description":"Fastweb: fibra o VDSL? Disinformazione alla porta",
            "extended":"",
            "hash":"d00d03acd6e01e9c2e899184eab35273",
            "href":"http:\/\/storify.com\/giovannibajo\/fastweb-fibra-o-vdsl",
            "private":"no",
            "shared":"yes",
            "tag":"",
            "time":"2013-06-14T10:30:08Z"
         }
      }
   ]
}

This is the function to grab stuff from it
Code: [Select]
$obj = $data_delicious['post']['@attributes'];

foreach (array_slice(json_decode($data_delicious, true), 0, 5) as $obj) {
    $delicious_title = str_replace('"', '\'', $obj['description']);
    $delicious_url = htmlentities($obj['href'], ENT_QUOTES, "UTF-8");
    $output = "<li><a rel=\"external nofollow\" title=\"$delicious_title\" href=\"$delicious_url\">$delicious_title</a></li>";
    echo $output;
}

Problem is I get these:
Illegal string offset 'post'
Illegal string offset '@attributes'
Undefined index: description
Undefined index: href