2014-10-27 157 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);