在PHP中创建JSON对象

时间:2014-10-27 16:14:33

标签: php json

我需要使用PHP创建一个JSON对象,因为我需要为每个节点提供属性,就像XML一样,我只能创建一个PHP数组加载(我认为),所以我正在创建PHP对象和这样做。

问题是我可以正确地将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"第一个节点具有属性" title":" Gallery"," entry":" 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/>

3 个答案:

答案 0 :(得分: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';

答案 1 :(得分:1)

在PHP中使用json的最简单方法是使用内置的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"}]

然后,您可以通过执行json_decode并像对象一样移动它来使用php再次访问这些内容。

我做了一个操场让你在这里弄乱: http://codepad.viper-7.com/qzMJO3

希望有所帮助!

答案 2 :(得分: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);