2014-10-27 49 views
0

我需要使用PHP創建JSON對象,因爲我需要爲像XML這樣的每個節點賦予屬性我不能只創建一個PHP數組(我認爲)的負載,所以我創建PHP對象,並以這種方式。在PHP中創建JSON對象

問題是我可以完全正確地格式化JSON。

這就是我想:

$object = new stdClass(); 

$object->{'0'}['title'] = 'Home'; 
$object->{'0'}['entry'] = '123'; 

$object->{'1'}['title'] = 'About'; 
$object->{'1'}['entry'] = '123'; 

$object->{'2'}['title'] = 'Gallery'; 
$object->{'2'}['entry'] = '123'; 

$object->{'2'} = new stdClass(); 

$object->{'2'}->{'0'}['title'] = 'Past'; 
$object->{'2'}->{'0'}['entry'] = '1234'; 

$object->{'2'}->{'1'}['title'] = 'Present'; 
$object->{'2'}->{'1'}['entry'] = '1235'; 

$object->{'2'}->{'0'} = new stdClass(); 

$object->{'2'}->{'0'}->{'0'}['title'] = '1989'; 
$object->{'2'}->{'0'}->{'0'}['entry'] = '12345'; 

$object->{'2'}->{'0'}->{'1'}['title'] = '1990'; 
$object->{'2'}->{'0'}->{'1'}['entry'] = '12346'; 


$ob=json_encode($object); 

echo $ob; 

,輸出:

{ 
"0":{"title":"Home","entry":"123"}, 
"1":{"title":"About","entry":"123"}, 
"2":{ 
"0":{ 
"0":{"title":"1989","entry":"12345"}, 
"1":{"title":"1990","entry":"12346"}}, 
"1":{"title":"Present","entry":"1235"} 
} 
} 

我需要 「2」 的第一個節點具有屬性 「稱號」 的: 「畫廊」,「項「:」123「,但也包含」過去「和」現在「的子節點,多年來也一樣。

在XML中,可能是這個樣子:

<0 title="Home" entry="123"> 
<0/> 
<1 title="About" entry="123"> 
<1/> 
<2 title="Gallery" entry="123"> 
    <0 title="Past" entry="1234"> 
    <0 title="1989" entry="12345"><0/> 
    <1 title="1990" entry="12346"><1/> 
    <0/> 
    <1 title="Present" entry="1235"> 
    <1/> 
<2/> 
+0

我不明白,爲什麼你不能只用數組這一點。 – vcanales 2014-10-27 16:23:18

+0

可能重複[簡單的jQuery,PHP和JSONP示例?](http://stackoverflow.com/questions/6809053/simple-jquery-php-and-jsonp-example) – MontrealDevOne 2014-10-27 16:39:30

+0

其實你是對的我可以devJunk。 – Kline 2014-10-27 16:47:05

回答

1

使用json和PHP最簡單的方法是使用內置的json_encode()和json_decode()函數。

這真的很好,因爲你可以將php數組直接編碼到json中,而無需執行任何操作!

$array = array(
    array(
     "title" => "Home", 
     "entry" => "123" 
    ), 
    array(
     "title" => "About", 
     "entry" => "123" 
    ), 
    array(
     "title" => "Gallery", 
     "entry" => "123", 
    ), 
); 

並不斷巢正因爲如此,你可以再轉化爲JSON對象:

$json = json_encode($array); 

有了這樣的輸出:

[{"title":"Home","entry":"123"},{"title":"About","entry":"123"},{"title":"Gallery","entry":"123"}] 

然後,您可以再次訪問這些PHP通過做一個json_decode並像一個對象一樣在它周圍移動。

我做了一個操場爲你惹這裏: http://codepad.viper-7.com/qzMJO3

希望幫助!

+1

原來你是對的 – Kline 2014-10-27 16:45:33

+0

如果這個答案有幫助,請務必將它標記爲接受的答案!謝謝 – acupajoe 2014-10-27 16:50:07

1

你與你的對象創建刪除它們:

交換解決這些線路:

$object->{'2'}['title'] = 'Gallery'; 
$object->{'2'}['entry'] = '123'; 
//this line creating the new object is effectively erasing the previous 2 lines. 
$object->{'2'} = new stdClass(); 

成爲:

$object->{'2'} = new stdClass(); 

$object->{'2'}['title'] = 'Gallery'; 
$object->{'2'}['entry'] = '123'; 
+0

試過了,它仍然沒有正確格式化 – Kline 2014-10-27 16:22:41

+0

發佈了你想要它看起來的json。 – Dave 2014-10-27 16:25:08

+0

不知道它怎麼樣我已經添加了XML等效 – Kline 2014-10-27 16:35:56

0

您正在設置$object->{'2'}$object->{'2'}->{'0'}new stdClass(),丟失了您之前設置的數據。

試試這個:

<?php 
$object = new stdClass(); 

$object->{'0'}['title'] = 'Home'; 
$object->{'0'}['entry'] = '123'; 

$object->{'1'}['title'] = 'About'; 
$object->{'1'}['entry'] = '123'; 

$object->{'2'}['title'] = 'Gallery'; 
$object->{'2'}['entry'] = '123'; 

$object->{'2'}['0']['title'] = 'Past'; 
$object->{'2'}['0']['entry'] = '1234'; 

$object->{'2'}['1']['title'] = 'Present'; 
$object->{'2'}['1']['entry'] = '1235'; 

$object->{'2'}['0']['0']['title'] = '1989'; 
$object->{'2'}['0']['0']['entry'] = '12345'; 

$object->{'2'}['0']['1']['title'] = '1990'; 
$object->{'2'}['0']['1']['entry'] = '12346'; 


$ob=json_encode($object);