You can't put 'bare' key/value pairs in an array, and reusing a key
like department isn't really good either. A simpler structure would
just be:

[ {"ID": 1, "Name": "Physics"}, {"ID": 2, "Name": "Chemistry"},
{"ID": 3, "Name": "Biology"} ]

Unless you have some compelling reason to assign the array to the
'departments' key in an object, you can just return the array from
a .getJSON() call

I believe you had a similar question on this post
http://groups.google.com/group/jquery-en/browse_thread/thread/e337d34bddd1af86/a1fd972050cda993#a1fd972050cda993
and the data structure you had there was fine, just missing quotes, as
another poster indicated.

You don't actually need to return true JSON, depending upon how you're
consuming the data (plain js is OK). This will work:

RETURN FROM getJSON:
[ {"Value": 1, "Item": "Physics"}, {"Value": 2, "Item": "Chemistry"},
{"Value": 3, "Item": "Biology"} ]


CALLING JS:
 $(function() {
        $.getJSON("data.js", function (json) {
                $('ul#depts').append( $.map(json,function(d,i){
                                        return '<li>'+d.Value+': 
'+d.Item+'</li>';
                                }).join('') );
        });
});

If you have a UL with the ID of 'depts' in your markup, it will be
populated with LI's containing the information you want. You can
structure your data differently if you like, such as making the array
the value in an object, and it will just mean accessing it
differently. But it makes sense to keep your data structure as simple
as possible, adding complexity only when your code demands it.

Here's a working copy of the above code if you'd like to see it; if
you don't already use it, get Firebug for Firefox, so you can log to
the console instead of alerting, and you can see the ajax request/
response in the console as well.
http://actingthemaggot.com/test/jquery_example/getjson.html

On Mar 26, 7:41 pm, iceangel89 <iceange...@gmail.com> wrote:
> ok so it works.
>
> so for my understanding, the parameters department refers to the index
> (0, 1, 2) and dictionary refers to the json object that i can use
> something like dictionary.ID on
>
> On Mar 27, 10:04 am, MorningZ <morni...@gmail.com> wrote:
>
> > Actually.. looking @ that JSON again after Steven's post, the JSON
> > should be
>
> > { "departments": [
> >    "department": {"ID": 1, "Name": "Physics"},
> >    "department": {"ID": 2, "Name": "Chemistry"},
> >    "department": {"ID": 3, "Name": "Biology"}
> >    ]
>
> > }
>
> > Pretty sure anyways... it's been a long day :-)
>
> > On Mar 26, 9:10 pm, Steven Yang <kenshin...@gmail.com> wrote:
>
> > > I think the problem is you have the "department" 3 times in the same hash
> > > object,so the last one overrides everything.
> > > you should change "departments" to an array and loop through it
>
> > > > > { "departments": {
> > > > >    "department": {"ID": 1, "Name": "Physics"},
> > > > >    "department": {"ID": 2, "Name": "Chemistry"},
> > > > >    "department": {"ID": 3, "Name": "Biology"}
> > > > > }}
>
> > > > > i tried
>
> > > > > $.each(json.departments.department, function(i, n) {
> > > > >       alert(i + " " + n);
> > > > > });

Reply via email to