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

145RE: [json] Accessing children of JSON Objects

Expand Messages
  • Atif Aziz
    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

      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


      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.


      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
      document.write('<div class="leaf" id="'+i+'">');
      } 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
      var txt1 = parse(tree1);

      Yahoo! Groups Links
    • Show all 4 messages in this topic