在PHP中访问JSON对象名称

xad*_*doc 10 php json

我有以下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}}}
Run Code Online (Sandbox Code Playgroud)

我拼命想要访问动态对象名称"-91607259\/387798111",我该怎么办呢?

现在我有:

$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>';
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ley 18

我想最简单的事情是解码为关联数组而不是stdClass对象

$obj2 = json_decode( $json, true );

foreach ( $obj2['streets'] as $coords => $street )
{   
  echo $coords;
}
Run Code Online (Sandbox Code Playgroud)


Pas*_*TIN 11

考虑到这段代码:

$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);
Run Code Online (Sandbox Code Playgroud)

你会得到 :

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
Run Code Online (Sandbox Code Playgroud)

这意味着你可以像这样在街道上循环:

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

有了它,对于每一个$street,你将获得相应的密钥$id- 和数据$street.


或者您可以通过以下方式直接访问:

$street = $obj2->streets->{'-86897820/399032795'};
var_dump($street);
Run Code Online (Sandbox Code Playgroud)

哪个会给你:

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


$obj2->street是一个对象,这意味着你不能使用数组语法访问; 这解释了" Fatal error: Cannot use object of type stdClass as array"如果您尝试使用此:

$obj2->streets['-86897820/399032795'];
Run Code Online (Sandbox Code Playgroud)

但是你的对象的属性有相当"奇怪"的名字; 这意味着你不能这样做:

$obj2->streets->-86897820/399032795;
Run Code Online (Sandbox Code Playgroud)

这使 Parse error: syntax error, unexpected '-', expecting T_STRING or T_VARIABLE or '{' or '$'

也不是:

$obj2->streets->'-86897820/399032795';
Run Code Online (Sandbox Code Playgroud)

这也给了 Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'

令人高兴的是,您可以使用{}"保护"密钥的名称,并使一切正常工作;-)
(我无法在手册中找到解释此语法的页面,也不会给出其名称......如果有人知道...... )