从JSON对象获取字符串

时间:2014-09-20 23:44:13

标签: javascript php json

我有一个名为$ graphData的JSON对象,当我使用<?php echo var_dump(json_decode($graphData)); ?>时,我得到以下内容:

object(stdClass)[987]
  public 'myself' => 
    object(stdClass)[984]
      public '1' => 
        object(stdClass)[986]
          public 'id' => string '999999999' (length=9)
          public 'value' => string '4.2' (length=3)
          public 'name' => string 'Myself' (length=6)
          public 'owner' => string '' (length=0)
          public 'type' => int 1
          public 'children' => 
            array (size=0)
              ...
  public 'my_teams' => 
    array (size=0)
      empty
  public 'my_units' => 
    array (size=0)
      empty
  public 'companies' => 
    array (size=1)
      0 => 
        object(stdClass)[982]
          public 'id' => string '66' (length=2)
          public 'name' => string 'Company' (length=8)
          public 'owner' => string 'Name Name' (length=13)
          public 'value' => string '4.2' (length=3)
          public 'type' => string '4' (length=1)
          public 'children' => 
            array (size=0)
              ...

如何访问标有&#39; value&#39;的字符串,以及值4.2?

由于

//编辑:我需要在php或js代码中使用它

1 个答案:

答案 0 :(得分:2)

在PHP中:

$data = json_decode($graphData);
$value = $data->companies[0]->value;
//Or for the one stored under "myself"
$value = $data->myself->{'1'}->value;

在JavaScript中:

var value = data.companies[0].value;
//Or for the one stored under "myself"
value = data.myself[1].value;