views:

39

answers:

1

I'm new to JSON and ajax, but i'm trying to access data in an array where the items are enumerated in a sub array within another sub array. I run into problems with I try to access something like

data.items[0].details.specs[1].name
data.items[0].details.specs[1].id
data.items[0].details.specs[2].name
data.items[0].details.specs[2].id
etc

Can anyone point me in the right direction on how to access this properly? Thanks.

Update: Here's a clarification to my question with what the JSON response looks like:

{data:{
     items: [
             {details: {specs:[{name:'something1', id:'something1'},
                               {name:'something2', id:'something2'},
                               {name:'something3', id:'something3'},
                               ...
                               ]}}
             {details: {specs:[{name:'somethingA', id:'somethingA'},
                               {name:'somethingB', id:'somethingB'},
                               {name:'somethingC', id:'somethingC'},
                               ...
                               ]}} 
             {details: {specs:[{name:'somethingX', id:'somethingX'},
                               {name:'somethingY', id:'somethingY'},
                               {name:'somethingZ', id:'somethingZ'},
                               ...
                               ]}}
            ]}
}

How would I access, for example, 'name:somethingB'?

+2  A: 

May be simply specs is not the array of objects.

Your json need to be like this to access that way

{ 
    data:{
         items: [
                 {details:
                      {specs:[
                              {name:'something1', id:'something1'},
                              {name:'something2', id:'something2'},
                              {name:'something3', id:'something3'},
                              ...
                              ]
                       }
                 } 
          ]
    }
}

Edit: It was browser cache issue according to OP

S.Mark
Hey thanks for responding. Yep, the array has multiple "items" arrays, and within each is a "specs" array. So how would I access 'name: something2' in your example? (whoop sorry, I'll clarify in an update to my question)
Archie Ec
@Archie, According to your update, looks like your JSON and your usage of JSON is fine, so it might be because of your browser cache (which reading old json, getting error if you access `data.items[0].details.specs[1].name`), so try clearing your browser cache and try again.
S.Mark
But one thing @Archie, make sure your array in json don't have extra comma at the end, for example `[{name:'name1',id:'id1'},{name:'name1',id:'id1'},]` some browser not working with that.
S.Mark
wow... i feel terrible after spending all day trying to figure this out, but ecstatic that you've solved my problem. Clearing the browser cache did it, thanks!
Archie Ec
You're welcome @Archie, glad to know that. :-)
S.Mark
And @Archie, sometimes, when you do AJAX calls, need to pass dummy parameters like `?1234` to avoid cache issue.
S.Mark
That's a good tip. I never thought of using a param like that for a JSON call. Thanks!
Archie Ec