oh, i understand now, thanks for explaining mkmanning


On Mar 27, 12:01 pm, mkmanning <michaell...@gmail.com> wrote:
> Sorry, that should have read "can't":
> which is basically my example. You CAN'T have elements in an array
> like
> this:
> [  "department": {"ID": 1, "Name": "Physics"} ]
>
> On Mar 26, 8:59 pm, mkmanning <michaell...@gmail.com> wrote:
>
> > This:
> > var departments = new Array();
> > var department = {};
> > department.ID = 1;
> > department.Name = "Physics";
> > deparments.push(department);
>
> > is not a non-shorthand version of this:
> > { "departments": [
> >    "department": {"ID": 1, "Name": "Physics"},
> >    "department": {"ID": 2, "Name": "Chemistry"},
> >    "department": {"ID": 3, "Name": "Biology"}
> > ]}
>
> > it's shorthand for this:
> > departments  = [ {"Value": 1, "Item": "Physics"}, {"Value": 2, "Item":
> > "Chemistry"}, {"Value": 3, "Item": "Biology"} ]
>
> > which is basically my example. You can have elements in an array like
> > this:
> > [  "department": {"ID": 1, "Name": "Physics"} ]
>
> > unless you wrap it in {}:
> > [ { "department": {"ID": 1, "Name": "Physics"}} ]
>
> > at which point the 'department' key name is not much use.
>
> > @iceangel89, did you check out my example?
>
> > On Mar 26, 8:47 pm, iceangel89 <iceange...@gmail.com> wrote:
>
> > > i am using PHP. intending to use Zend Framework - still PHP
>
> > > On Mar 27, 11:38 am, MorningZ <morni...@gmail.com> wrote:
>
> > > > Talking about my first post?
>
> > > > I'm not so sure the JSON you posted
>
> > > > { "departments": {
> > > >    "department": {"ID": 1, "Name": "Physics"},
> > > >    "department": {"ID": 2, "Name": "Chemistry"},
> > > >    "department": {"ID": 3, "Name": "Biology"}
>
> > > > }}
>
> > > > is valid, as Steven points out, the last "department" wins....  even
> > > > if "department" was "department1", "department2", "departmentN", you
> > > > still wouldn't be able to reference the items by a positional index
> > > > (see the link at end of post about "associative arrays")
>
> > > > but
>
> > > > { "departments": [
> > > >    "department": {"ID": 1, "Name": "Physics"},
> > > >    "department": {"ID": 2, "Name": "Chemistry"},
> > > >    "department": {"ID": 3, "Name": "Biology"}
> > > > ]}
>
> > > > makes more sense perhaps if you see the non short hand version of it
>
> > > > var departments = new Array();
>
> > > > var department = {};
> > > > department.ID = 1;
> > > > department.Name = "Physics";
> > > > deparments.push(department);
>
> > > > department = {};   //Not 100% sure if this leaves the orig obj at
> > > > position 0)
> > > > department.ID = 2;
> > > > department.Name = "Chemistry";
> > > > deparments.push(department);
>
> > > > department = {};
> > > > department.ID = 3;
> > > > department.Name = "Biology";
> > > > deparments.push(department);
>
> > > > would (pretty sure as i rattled that off the top of my head) be
> > > > equivalent to the 2nd JSON example
>
> > > > This should help explain a little better
>
> > > >http://www.pageresource.com/jscript/jarray2.htm
>
> > > > I hope that points you in the right direction
>
> > > > What server side language are you using btw?  there's a JSON
> > > > converting tool for all the common languages... it takes ALL the
> > > > guesswork out of this stuff
>
> > > > On Mar 26, 10: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