PDA

View Full Version : Multidimensional JSON from PHP



alessandro
18 Aug 2009, 5:26 AM
Hi all.
I'm having trouble generating a JSON array from PHP.

I need a tree like this:



level1
level2
level3
level3
level3
level2


Hi have the following JSON code made from PHP:


[{"text":"DNS","leaf":"false","children":[{"text":"Forwarding zone","leaf":"false","children":[[{"text":"testdomain.com","leaf":"true","id":"domain_fwd_2","children":""},{"text":"mytest.it","leaf":"true","id":"domain_fwd_1","children":""}]]},{"text":"Reverse zone","leaf":"false","children":[[{"text":"testdomain.com","leaf":"true","id":"domain_rev_2","children":""},{"text":"anothertest.org","leaf":"true","id":"domain_rev_1","children":""}]]}]}]


That code doesn't work.
I'm generating it with "json_encode" with php 5.2.

This is the original PHP array:


Array
(
[text] => DNS
[leaf] => false
[children] => Array
(
[0] => Array
(
[text] => Forwarding zone
[leaf] => false
[children] => Array
(
[0] => Array
(
[0] => Array
(
[text] => testdomain.com
[leaf] => true
[id] => domain_fwd_2
[children] =>
)

[1] => Array
(
[text] => mytest.it
[leaf] => true
[id] => domain_fwd_1
[children] =>
)

)

)

)

[1] => Array
(
[text] => Reverse zone
[leaf] => false
[children] => Array
(
[0] => Array
(
[0] => Array
(
[text] => testdomain.com
[leaf] => true
[id] => domain_rev_2
[children] =>
)

[1] => Array
(
[text] => anothertest.org
[leaf] => true
[id] => domain_rev_1
[children] =>
)

)

)

)

)

)


If you would like to try, this is the serialized array


a:3:{s:4:"text";s:3:"DNS";s:4:"leaf";s:5:"false";s:8:"children";a:2:{i:0;a:3:{s:4:"text";s:18:"Forwarding zone";s:4:"leaf";s:5:"false";s:8:"children";a:1:{i:0;a:2:{i:0;a:4:{s:4:"text";s:19:"testdomain.com";s:4:"leaf";s:4:"true";s:2:"id";s:12:"domain_fwd_2";s:8:"children";s:0:"";}i:1;a:4:{s:4:"text";s:12:"mytest.it";s:4:"leaf";s:4:"true";s:2:"id";s:12:"domain_fwd_1";s:8:"children";s:0:"";}}}}i:1;a:3:{s:4:"text";s:15:"Reverse zone";s:4:"leaf";s:5:"false";s:8:"children";a:1:{i:0;a:2:{i:0;a:4:{s:4:"text";s:19:"testdomain.com";s:4:"leaf";s:4:"true";s:2:"id";s:12:"domain_rev_2";s:8:"children";s:0:"";}i:1;a:4:{s:4:"text";s:12:"anothertest.org";s:4:"leaf";s:4:"true";s:2:"id";s:12:"domain_rev_1";s:8:"children";s:0:"";}}}}}}

steffenk
18 Aug 2009, 5:35 AM
the produced json is correct, validate it! (http://www.jsonlint.com/) So the problem seems to be on reading side.

alessandro
18 Aug 2009, 5:38 AM
I'm working on a heavy customized version of Ext's Layout sample.

the code that load the json tree is the following:



var treePanel = new Ext.tree.TreePanel({
id: 'tree-panel',
title: 'My Title',
region:'center',
split: true,
//minSize: 265,
autoScroll: true,
//height: 265,
// tree-specific configs:
rootVisible: false,
singleExpand: false,
useArrows: true,

loader: new Ext.tree.TreeLoader({
dataUrl:'http://myserver.tld/test.php'
,requestMethod: 'GET'
}),

root: new Ext.tree.AsyncTreeNode()
});


With another JSON object tree is shown correctly, but jsonlint validator doesn't validate it. :)

steffenk
18 Aug 2009, 5:40 AM
try POST, GET is limited to a specific length

alessandro
18 Aug 2009, 5:43 AM
Ok, but the working json is much bigger than this.

alessandro
18 Aug 2009, 6:10 AM
Even a very simple JSON like the following doesn't work


[{"text":"DNS","leaf":"false","children":""}]


The tree is shown, but i'm unable to view the leaf nodes.
"DNS" is set as a leaf instead as a node.

alessandro
18 Aug 2009, 7:05 AM
Someone can tell me what's wrong with this JSON?



[
{
"text": "Pagina iniziale"
,"leaf": "true"
}
,{
"text": "DNS"
,"leaf": "false"
,"expanded": "true"
,"children":
[
{
"text": "Forward"
,"leaf": "false"
,"children":
[
{
"text": "aliasdominioDNS.com"
,"leaf": "true"

}
,{
"text": "asdasda.com"
,"leaf": "true"

}
]

},
{
"text": "Reverse"
,"leaf": "false"
,"children":

[
{
"text": "aliasdominioDNS.com"
,"leaf": "true"

}
,{
"text": "asdasdasd.com"
,"leaf": "true"

}
]

}
]
}
]


Latest node redered is "DNS" with no child.

httpdotcom
18 Aug 2009, 7:54 AM
shouldn't each node have a unique id config? (like first example code @ http://extjs.com/deploy/dev/docs/output/Ext.tree.TreeLoader.html)



[ { id: 1,
text: "1",
children: [
{ id: 2,
text: "2",
children: [
{ id: 4,
text: "4",
leaf: true }
]
}
,{ id: 3,
text: "3",
leaf: true }
]
} ]


also, don't quote your booleans (true/false).