11

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>';
}

4 Answers 4

20

I would imagine that the simplest thing to do is to decode into associative arrays instead of stdClass objects

$obj2 = json_decode( $json, true );

foreach ( $obj2['streets'] as $coords => $street )
{   
  echo $coords;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Using associative arrays "json_decode( $json, true );" gave me this error "Warning: Invalid argument supplied for foreach()" but the "foreach ( $obj2->streets as $coords => $street )" worked like a charm! :D Thank you very much! Also thanks to Gumbo for the code edit. Stackoverflow is really great!
Haha, I suggested switching to associative arrays and then promptly forgot to use that syntax in my snippet. Answer updated. Thanks for the catch ;)
14

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... )

1 Comment

foreach ( $obj2->streets as $id => $street ) was the piece missing. Thank you for your help and explanation!
2

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!

Comments

0

I can´t try it right now, but if you do a:

var_dump($obj2);

you should be able to see exactly how to access your information.

1 Comment

Also with Firebug you have a really nice JSON "explorer" where you can see and navigate the whole structure and data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.