Search code examples
phpjsonecho

Decode JSON with square brackets


I have tried to decode this json,but no luck,these square brackets are making me confused any help would be appreciated,here is my json

[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]

Thank you


Solution

  • try this:

    var_export( json_decode( '[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]' )  );
    

    json_decode return array or object . you can print it with var_export not echo

    and you can access to values :

    $items = json_decode('[{"location":[{"building":["Default Building"],"name":"Default Location"}],"name":"Default Organization"}]');
    
    foreach( $items as $each ){
      echo $each->location[0]->building[0];
      echo '<hr />';
      echo $each->location[0]->name;
      echo '<hr />';
      echo $each->name; // default organization
    }