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

RE: [json] Accessing children of JSON Objects

Expand Messages
  • Atif Aziz
    The value of i in the for/in loop is a string that represents the name of the object s property being enumerated for that iteration. You need to index the
    Message 1 of 4 , Dec 21, 2005
    • 0 Attachment
      The value of i in the for/in loop is a string that represents the name
      of the object's property being enumerated for that iteration. You need
      to index the property name back into the object to get the value. In
      other words use brs[i].text instead of just i.text.

      - Atif

      -----Original Message-----
      From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of
      Paul Hanlon
      Sent: Wednesday, December 21, 2005 4:00 PM
      To: json@yahoogroups.com
      Subject: [json] Accessing children of JSON Objects

      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);





      Yahoo! Groups Links
    • pthanlonie
      Hi, Thanks so much. I ve been tearing my hair out. When I tried brs.i.text, and it didn t work, I never thought to use the brackets. I ll need to rejig my JSON
      Message 2 of 4 , Dec 21, 2005
      • 0 Attachment
        Hi,

        Thanks so much. I've been tearing my hair out. When I tried
        brs.i.text, and it didn't work, I never thought to use the brackets.
        I'll need to rejig my JSON object to get rid of the horrible condition
        check, and also to stop it trying to parse other objects within each
        parent, but that's easy to do, given the flexibility that JSON allows
        (That's why I liked it so much in the first plsce).

        Thanks again,

        Paul.
      Your message has been successfully submitted and would be delivered to recipients shortly.