在PHP中访问JSON对象名称

时间:2009-09-25 19:02:31

标签: 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}}}

我拼命想要访问动态对象名称,例如"-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>';
}

4 个答案:

答案 0 :(得分:17)

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

$obj2 = json_decode( $json, true );

foreach ( $obj2['streets'] as $coords => $street )
{   
  echo $coords;
}

答案 1 :(得分: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);

你会得到:

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

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

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

这样,对于每个$street,您都会将相应的密钥转换为$id - 并将数据转换为$street


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

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

哪能得到你:

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


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

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

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

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

给出了Parse error: syntax error, unexpected '-', expecting T_STRING or T_VARIABLE or '{' or '$'

也不是:

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

还提供了Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'

令人高兴的是,您可以使用{}来“保护”您的密钥名称,让一切正常运行;-)
(我在手册中找不到解释这种语法的页面,也没有给出它的名字......如果有人知道......)

答案 2 :(得分:0)

我现在不能尝试,但如果你这样做:

var_dump($obj2);

您应该能够确切了解如何访问您的信息。

答案 3 :(得分:0)

Json string提取数据的一种简单方法是使用jsone_decode()函数。例如:

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

然后,您可以使用$decoded_data运算符简单地访问->的成员:

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

如果您从$decoded_data提取的成员又是json string,那么您可以再次使用json_decode()