Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access JSON object name in PHP

Tags:

json

php

I have the following JSON:

{
  "nickname": "xadoc",
  "level": 4,
  "loc": "Tulsa, OK, USA",
  "score": 122597,
  "money": 29412.5,
  "streetNum": 8,
  "streets": {
    "-91607259\/387798111": {
      "name": "Alam\u00e9da Ant\u00f3nio S\u00e9rgio",
      "value": 243,
      "type": 1
    },
    "-91016823\/388182402": {
      "name": "Autoestrada do Norte",
      "value": 18304,
      "type": 1
    },
    "-86897820\/399032795": {
      "name": "Autoestrada do Norte",
      "value": 12673,
      "type": 1
    },
    "-973092846\/479475465": {
      "name": "19th Ave",
      "value": 7794,
      "type": 1
    },
    "-974473223\/480054888": {
      "name": "23rd Ave NE",
      "value": 33977,
      "type": 1
    }
  }
}

I'm desperately trying to access the dynamic object names like "-91607259\/387798111", how can I do it?

Right now I have:

$jsonurl = "http://www.monopolycitystreets.com/player/stats?nickname=$username&page=1";
$json = file_get_contents($jsonurl,0,null,    $obj2 = json_decode($json);

foreach ( $obj2->streets as $street )
{   
    //Here I want to print the "-91607259\/387798111" for each street, please help
    //echo $street[0]; gives "Fatal error: Cannot use object of type stdClass as array"
    //echo $street gives "Catchable fatal error: Object of class stdClass could not be converted to string"
    echo '<th>'.$street->name.'</th><td>'."M ".number_format($street->value, 3, ',', ',').'</td>';
}
like image 901
xadoc Avatar asked Sep 25 '09 19:09

xadoc


People also ask

How are JSON values access in PHP?

To receive JSON string we can use the “php://input” along with the function file_get_contents() which helps us receive JSON data as a file and read it into a string. Later, we can use the json_decode() function to decode the JSON string.

How do you store a JSON in a variable in PHP?

PHP File explained:Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data. Add the array to an object, and return the object as JSON using the json_encode() function.


2 Answers

Considering this piece of code :

$json = '{"nickname":"xadoc","level":4,"loc":"Tulsa, OK, USA","score":122597,"money":29412.5,"streetNum":8,"streets":{"-91607259\/387798111":{"name":"Alam\u00e9da Ant\u00f3nio S\u00e9rgio","value":243,"type":1},"-91016823\/388182402":{"name":"Autoestrada do Norte","value":18304,"type":1},"-86897820\/399032795":{"name":"Autoestrada do Norte","value":12673,"type":1},"-973092846\/479475465":{"name":"19th Ave","value":7794,"type":1},"-974473223\/480054888":{"name":"23rd Ave NE","value":33977,"type":1}}}';
$obj2 = json_decode($json);
var_dump($obj2);

You'll get :

object(stdClass)[1]
  public 'nickname' => string 'xadoc' (length=5)
  public 'level' => int 4
  public 'loc' => string 'Tulsa, OK, USA' (length=14)
  public 'score' => int 122597
  public 'money' => float 29412.5
  public 'streetNum' => int 8
  public 'streets' => 
    object(stdClass)[2]
      public '-91607259/387798111' => 
        object(stdClass)[3]
          public 'name' => string 'Alaméda António Sérgio' (length=25)
          public 'value' => int 243
          public 'type' => int 1
      public '-91016823/388182402' => 
        object(stdClass)[4]
          public 'name' => string 'Autoestrada do Norte' (length=20)
          public 'value' => int 18304
          public 'type' => int 1
      public '-86897820/399032795' => 
        object(stdClass)[5]
          public 'name' => string 'Autoestrada do Norte' (length=20)
          public 'value' => int 12673
          public 'type' => int 1
      public '-973092846/479475465' => 
        object(stdClass)[6]
          public 'name' => string '19th Ave' (length=8)
          public 'value' => int 7794
          public 'type' => int 1
      public '-974473223/480054888' => 
        object(stdClass)[7]
          public 'name' => string '23rd Ave NE' (length=11)
          public 'value' => int 33977
          public 'type' => int 1

Which means you can loop over the streets like this :

foreach ( $obj2->streets as $id => $street ) {
    echo $id;
    var_dump($street);
    echo '<hr />';
}

With this, for each $street, you'll get the corresponding key into $id -- and the data into $street.


Or you can directly access one this way :

$street = $obj2->streets->{'-86897820/399032795'};
var_dump($street);

Which will get you :

object(stdClass)[5]
  public 'name' => string 'Autoestrada do Norte' (length=20)
  public 'value' => int 12673
  public 'type' => int 1


Your $obj2->street is an object, which means you cannot use array-syntax access ; this explains the "Fatal error: Cannot use object of type stdClass as array" if you try to use this :

$obj2->streets['-86897820/399032795'];

But the properties of your object have quite "strange" names ; which means you cannot do this :

$obj2->streets->-86897820/399032795;

Which gives Parse error: syntax error, unexpected '-', expecting T_STRING or T_VARIABLE or '{' or '$'

Nor that :

$obj2->streets->'-86897820/399032795';

Which also gives Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'

Happily, you can use {} to kind of "protect" the name of your keys, and get everything working ;-)
(I cannot find the page in the manual that explains this syntax nor give its name... If anyone know... )

like image 129
Pascal MARTIN Avatar answered Nov 13 '22 04:11

Pascal MARTIN


A simple way to extract data from Json string is to use jsone_decode() function. For example:

$json_data = '{"nickname":"xadoc","level":4,"loc":"Tulsa}';
$decoded_data = json_decode($json_data);

Then you can simply access the members of $decoded_data using -> operator:

echo "$decoded_data->nickname \n";
echo "$decoded_data->level \n";

And if your extracted members from $decoded_data are json strings again, then you can use json_decode() again!

like image 32
Hamid Araghi Avatar answered Nov 13 '22 03:11

Hamid Araghi