tags:

views:

26

answers:

1

I have been trying to access certain part of the json response, however I am unable to do so.

json = '{

"now": "2010-09-23 22:06:53 EST",

"data":[

{"id":"1","year":"2010","month":"09","day":"23","hours":"08","minutes":"09","seconds":"25"},{"id":"8","year":"2010","month":"09","day":"23","hours":"08","minutes":"09","seconds":"18"},    {"id":"3","year":"2010","month":"09","day":"23","hours":"08","minutes":"09","seconds":"24"},{"id":"4","year":"2010","month":"09","day":"23","hours":"08","minutes":"09","seconds":"30"}]}';

I am parsing my data using this:

var resp = jQuery.parseJSON(json);
alert(resp.data);

How can I access month of id 3 for instance?

Thanks

I have tried alert(resp.data[0]) but no go.

+2  A: 

try

$.each(resp.data,function(i,v){
    if(v.id == 3) {
        alert(v.month) // alerts "09"
    }
});
Reigel
beautiful, thank you very much!
Bastien
don't really need to do that - resp.data[0].month will work.
RPM1984
@RPM1984 the OP want it to get the month based on `id`.
Reigel
Ah true, he did. Didnt read that properly, my bad. lucky i didnt answer :)
RPM1984
@Bastien - you're welcome. just accept it as the correct answer if it is. ;)
Reigel
@RPM1984 thanks anyways, I was also looking for your that kind of answer.
Bastien