PDA

View Full Version : Parsing the response from an ajax request



craigwalshuk
22 Aug 2011, 6:13 AM
I am trying to create "daily quote" style functionality using an ajax request to a local json file. I am getting a successful response but I am having trouble parsing the information.

The quotes are set out by month in the json file (I have not included all the days to keep the code examples short) and I'm using the date function to get the current date and pull out the correct quote. Please find below the json file and the handler for a button the user presses to initiate the ajax request.



{
"january":
[
{"author": ""},
{"quote": ""}
],
"february":
[
{"author": ""},
{"quote": ""}
],
"march":
[
{"author": ""},
{"quote": ""}
],
"april":
[
{"author": ""},
{"quote": ""}
],
"may":
[
{"author": ""},
{"quote": ""}
],
"june":
[
{"author": ""},
{"quote": ""}
],
"july":
[
{"author": ""},
{"quote": ""}
],
"august":
[
{"author": ""},
{"quote": ""}
],
"september":
[
{"author": ""},
{"quote": ""}
],
"october":
[
{"author": ""},
{"quote": ""}
],
"november":
[
{"author": ""},
{"quote": ""}
],
"december":
[
{"author": ""},
{"quote": ""}
]
}


and here is the javascript:



{
text: 'Load',
handler: function() {
var panel = quote; // This is the panel that displays the quotes

panel.update('');
panel.setLoading(true, true);

Ext.Ajax.request({
url: 'app/bookQuote.json',
callbackKey: 'callback',


callback: function(response) {
var today = new Date();
var month = today.getMonth();
var day = today.getDate();

switch (month) {
case 0: var bookQuoteMonth = response.january; break;
case 1: var bookQuoteMonth = response.february; break;
case 2: var bookQuoteMonth = response.march; break;
case 3: var bookQuoteMonth = response.april; break;
case 4: var bookQuoteMonth = response.may; break;
case 5: var bookQuoteMonth = response.june; break;
case 6: var bookQuoteMonth = response.july; break;
case 7: var bookQuoteMonth = response.august; break;
case 8: var bookQuoteMonth = response.september; break;
case 9: var bookQuoteMonth = response.october; break;
case 10: var bookQuoteMonth = response.november; break;
case 11: var bookQuoteMonth = response.december; break;
}

panel.update(bookQuoteMonth[day]);
}
});
}
}

craigwalshuk
26 Aug 2011, 1:07 AM
Any help on this would be appreciated.