Loading ...
Sorry, an error occurred while loading the content.

144Re: [json] Accessing children of JSON Objects

Expand Messages
  • Michal Migurski
    Dec 21, 2005
    • 0 Attachment
      The javascript for/in syntax actually iterates over the properties of
      an object or indexes of an array. Javascript's "for(index in array)"
      is roughly equivalent to PHP's "foreach($array as $index =>
      $element)", with the elements accessible via "array[index]". So where
      you do your recursive call, pass brs[i], not just i.

      -mike.

      On Dec 21, 2005, at 6:59 AM, Paul Hanlon wrote:

      > Hi All,
      > I wonder if anyone can help.I come from a background of programming in
      > PHP, and am only just beginning to get to grips with the power of
      > Javascript. I'm trying to build a tree object that will be populated
      > from the server using JSON. This is a simplified version of the
      > JSON object.
      >
      > var tree1 = {
      > "br1": {"text": "Branch 1",
      > "lf1": {"text": "Leaf 1"},
      > "lf2": {"text": "Leaf 2"},
      > "br2": {"text": "Branch 2",
      > "lf3": {"text": "Leaf 3"},
      > "br3": {"text": "Branch 3",
      > "lf4": {"text": "Leaf 4"}
      > }
      > },
      > "br4": {"text": "Branch 4",
      > "lf5": {"text": "Leaf 5"}
      > }
      > },
      > "br5": {"text": "Branch 5",
      > "lf6": {"text": "Leaf 6"}
      > },
      > "lf7": {"text": "Leaf 7"}
      > };
      > To get to the deepest level of this tree, I can just do
      >
      > alert(tree1.br1.br2.br3.lf4.text);
      >
      > and it returns "Leaf 4"
      >
      > My problem arises when I try to do things within a recursive
      > function. I
      > use a for in loop to access the children of the object, and first time
      > around, if I do alert(i), I get the three top level nodes, br1,
      > br5, and
      > lf7 along with toSource and quote for each child. Furthermore i.text
      > returns "undefined", so I can't access i's children and the function
      > fails. The recursion could be wrong, but I can't even test for that,
      > until I can work out how to get the children. Can anybody tell me what
      > I'm doing wrong?
      > I could fall back to doing most of this from the server, but JSON just
      > struck me as a really elegant way of doing this, as the finished
      > object
      > will be substantially more complex and will need to be updateable
      > dynamically. Any help would be greatly appreciated.
      >
      > Paul.
      >
      > function parse(brs){
      > for(i in brs){
      > if(i.substr(0, 2)=="br") {
      > document.write('<span class="branch"><a href="javascript:
      > toggle(\''+i+'\');" title="Click to show/hide children"><img
      > src="closed.gif" border="0" id="I'+i+'"></a><a href="javascript:
      > show(\'help\')" title="Click for further options">'+i.text+'</a></
      > span>');
      > document.write('<div class="leaf" id="'+i+'">');
      > document.write(parse(i));
      > document.write('</div>');
      > } else if(i.substr(0, 2)=="lf") {
      > document.write('<img src="doc.gif" border="0"><a
      > href="javascript: show(\'help\')" class="" title="Click for further
      > options">'+i.text+'</a><br>');
      > }
      > }
      > }
      > var txt1 = parse(tree1);
      >
      >
      >
      > SPONSORED LINKS
      > Programming languages Format Computer security
      > Computer training Large format Cover letter formats
      >
      > YAHOO! GROUPS LINKS
      >
      > Visit your group "json" on the web.
      >
      > To unsubscribe from this group, send an email to:
      > json-unsubscribe@yahoogroups.com
      >
      > Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
      >
      >

      ------------------------------------------------------
      michal migurski- contact info, blog, and pgp key:
      sf/ca http://mike.teczno.com/contact.html



      [Non-text portions of this message have been removed]
    • Show all 4 messages in this topic