2010-11-12 86 views
2

我想用我的數據使用json + php。我讀了更多的文檔來做到這一點,基本功能是json_decode()和json_encode()。我的問題是,閱讀更多文檔和閱讀結構的不同例子在我身上產生了很多疑問。如何在PHP中以'複雜'結構使用json?

我想創建這樣begine從基本到容器的結構:

  • 有一個基地,即有2個屬性:ID和值
  • 有一個可以有多個基地一個操作
  • 有一個命令,可以有多個操作(如果可能的屬性callad名)

在我心中的結構是這樣的...

[ //The start of Commands 

    //Can make a property name here like "name":"puls1" 
    [ //Operation1 
{ //Base1 
    "id":"22398", 
    "value":"255" 
}, 
{ //Base2 
    "id":"22657", 
    "value":"80", 
}, 
{ //Base3 
    "id":"7928", 
    "valore":"15" 
} 
    ], 

    [ //Operation2 
{ //Base1 
    "id":"22398", 
    "value":"0" 
}, 
{ //Base2 
    "id":"22657", 
    "value":"0", 
}, 
{ //Base3 
    "id":"7928", 
    "valore":"0" 
} 
    ], 

] //The close of Commands 

但我已經把[和{以不正確的順序,我想... 我怎樣才能使這樣的json結構?並設置一個命令後插入一個新的操作或刪除操作?

感謝的所有..

// OK回答時我做了這個代碼

class Base 
{ 
    var $i; 
    var $value; 

    function __construct($i,$v) 
    { 
    $this->id = $i; 
    $this->value = $v; 
    } 
} 

$a = new Base('1','11'); 
$b = new Base('2','10'); 
$c = new Base ('3','20'); 
$d = new Base ('4','30'); 

class Operation 
{ 
    var $name; 
    var $values = Array(); 

    function __construct($a) 
    { 
    $this->name = $a; 
    } 

    public function addArray($a) 
    { 
    array_push($this->values,$a); 
    } 
} 

$oper1 = new Operation("op1"); 
$oper1->addArray($a); 
$oper1->addArray($b); 

$oper2= new Operation("op2"); 
$oper2->addArray($c); 
$oper2->addArray($d); 

$commands = Array($oper1,$oper2); 

echo json_encode($tot); 

現在的問題是如何讓我的復歸操作?這種json_decode的使用和適當的結構包裝?

回答

2

如果你使用PHP工作,我會從構建本地的PHP類對象(json_encode可與PHP對象以及):

class Base { 
    var $id; 
    var $value; 
} 

然後,只需要將這些對象放入各種數組中,您也可以使用諸如addToOperation($baseObj)addToCommands($operationObj)之類的方法進行抽象。

您正在處理本機數據結構(Arrays),因此您可以使用本地方法刪除(array_pop)和添加(array_push)數據。

+0

與您的代碼我試試這個類的基地 \t { \t變量$ ID; \t變量$值; \t \t function __construct($ i,$ v) \t { \t \t $ this-> id = $ i; \t \t $ this-> value = $ v; \t} \t} \t \t $ a = new Base('1','11'); \t $ b = new Base('2','10'); \t \t \t $ commans1 = Array($ a,$ b); \t $ commans2 = Array($ a,$ b); \t \t $ tot = Array($ commans1,$ commans2); \t \t echo json_encode($ tot); 如何添加屬性名稱到操作? – user473349 2010-11-12 09:57:35

1

像這樣的東西應該工作

// Build up your data as a mulitdimensional array 
$data = array(
    'operations' => array(
     0 => array(
      'bases' => array (
      0 => array(
       'id' => '22398', 
       'value' => 'whatever' 
      ), 
      1 => array(
       'id' => 'id goes here', 
       'value' => 'value goes here' 
      ), 
     1 => array(
      //data for operation 2 
     ) 
); 

// Then use json_encode 
$json = json_encode($data); 

我的語法可能不是完美的,但應該給你的想法。要訪問它,那麼你會使用代碼如

$operations = json_decode($data); 
foreach ($operations as $op) { 
    foreach ($op->bases as $base) { 
     //Logic goes here 
     } 
} 

希望這會有所幫助。

+0

是的,但我想要一個完整的經理例子結構...例如我可以如何添加另一個命令? – user473349 2010-11-12 09:16:49

+0

只是把命令放在其他地方$ data = array('commands'=> array(0 => array)。我認爲這是最好的管理,你可以想出 – Belinda 2010-11-12 09:22:09

+0

如果你想給命令一個名字,可以在操作之前添加名稱作爲命令的屬性將您的結構視爲多維數組,構建該數組並使用json_encode是複雜數據的最佳技術 – Belinda 2010-11-12 09:32:12

4

json列表類型[]等於在PHP中沒有鍵的數組。

json字典類型{}等於在php中的鍵控數組。

你需要的是這樣的:

$json = array(
    array(
    array('id' => $num, 'value' => $val), // Base 1 
    array('id' => $num_1, 'value' => $val_1), // Base 3 
    array('id' => $num_2, 'value' => $val_2), // Base 2 
    ), 
    array(...), 
    array(...), 
); 
+0

非常好的代碼...如果我想要一個值一個可以使用$ json [0] [0] - > id;只有一個問題是否有一種方法來添加一個屬性到數組(...)在我的情況下的操作? – user473349 2010-11-12 09:40:59