Hi,

You don't need a Hash, the "children" objects are Arrays, which means
they'll have the Enumerable#each[1] function mixed in with them.

Also, if you're using Prototype 1.6 and returning the correct content-
type, you don't need to use transport.responseText.evalJSON -- just
use transport.responseJSON, it's already been done.  But regardless of
which way you do it, eventually you end up with a 'json' var pointing
to that structure.

If you already know that you'll be getting back a category
("Products") with one sub-category ("Furniture") and that you want to
loop through the children of that sub-category, you can get a
reference to them like so:

var children = json.children[0].children;

If not (if id:3 may be anywhere in the structure), you'll have to go
looking for it, perhaps with recursive descent, something like this
(largely untested):

* * * *
function findEntry(obj, id) {
    var rv, children, n;

    if (obj.id == id) {
        rv = obj;
    } else {
        rv = undefined;
        children = obj.children;
        if (children && children.length > 0) {
            for (n = children.length - 1; !rv && n >= 0; --n) {
                rv = findEntry(children[n], id);
            }
        }
    }
    return rv;
}
* * * *

Once you've found the object that has the children, looping through
them is trivial.  Either a boring old-fashioned loop like the one in
findEntry above, or you could use Enumerable#each like this:

* * * *
obj.children.each(function(child) {
    alert(child.name);
});
* * * *

So putting it all together:

* * * *
onSuccess: function(transport) {

    var json = transport.responseJSON;
    var obj = findEntry(json, 3);
    if (obj) {
        obj.children.each(function(child) {
            alert(child.name);
        });
    }
}
* * * *

You might also want to look at some of the other Enumerable methods,
like #pluck[2], depending on what it is you want to do with the child
names.

[1] http://prototypejs.org/api/enumerable/each
[2] http://prototypejs.org/api/enumerable/pluck

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jul 6, 2:09 am, seamusjr <[email protected]> wrote:
> I am trying to get a collection of objects in JSON that have children
> and iterate through the name properties of those children.  For
> example I want to get the collection of Furniture and return its
> children.
>
> I have tried to do this by converting the Object to a Hash and iterate
> through it with an each.  It seems to get the Objects but returns them
> as undefined.
>
> How can I return the names of the children in id:3 as strings?
>
> Thanks,
> Seamus
>
> sample.json:
>
> {
>         "id":         2,
>         "name": "Products",
>         "children": [
>                 { "id": 3, "name": "Furniture", "children":
>                 [
>                         { "id": 4, "name": "Table", children: []},
>                         { "id": 5, "name": "Chair", children: []},
>                         { "id": 6, "name": "Lamp", children: []},
>
>                 ]
>                 }
>         ]
>
> }
>
> ajax.js:
>
> var SomeObj = Class.create();
> SomeObj.prototype = {
>    initialize: function(){
>         console.log("Hello");
>    },
>
> getStuff: function(){
>  new Ajax.Request('/sample.json',
>  {
>         method:'get',
>         onSuccess: function(transport){
>         var json = transport.responseText.evalJSON();
>                 $H(json).each(function(element){
>                  console.log(json.children.name);
>           });
>
>     },
>
>     onFailure: function(){ console.log('Something went wrong...') }
>     });
>
> }
>
>         }
>
> Event.observe(document, "dom:loaded", function(){
>     someObj = new SomeObj;
>     someObj.getStuff();
>
>
>
> });
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to