2014-09-20 237 views
0

字符串我有一個名爲$ graphData一個JSON對象,當我使用<?php echo var_dump(json_decode($graphData)); ?>我得到如下:獲取從JSON對象

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

如何訪問標有「價值」的字符串,並與值4.2?

感謝

//編輯:我需要在PHP或JS代碼

+2

那麼你嘗試什麼?這是兩種語言中的一項微不足道的任務。 – Qix 2014-09-20 23:46:25

+0

你有沒有嘗試迭代「公司」? – 2014-09-20 23:47:13

回答

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; 
+0

是的,我正在尋找「我自己」下的那個。謝謝! – nullbuilt 2014-09-21 00:03:29