PDA

View Full Version : Having problem with decoding a Json response



naddaf
27 Jul 2010, 11:05 AM
Hello everyone.

I am running into an issue that should be trivial but for some reason I can't get it to work. Let's say I have the following (string) data:

var dataString = '[{content:"Please remember to take your medication", source:"T. Spencer"}, {content:"Please setup an office consultation", source:"T. Spencer"}]';

If I want to create a Json object (array) from it, in Ext JS, I used to do:

var data = Ext.decode(dataString);

and then data would be a JS object. But in Sencha Touch, this last call doesn't work. Could some one please tell me what I am doing wrong? My final goal is to pass this data object to a template like the following:

'<tpl for=".">',
'<h2>{source}</h2>',
'<p>{content}</p>',
'</tpl>'

Many thanks,
Ali.

hat27533
27 Jul 2010, 12:25 PM
Could the full stop be the problem in "T. Spencer" ?

If the dataString is originated from JavaScript why not make it an object in the first instance?

Where does dataString come from ?

naddaf
27 Jul 2010, 1:42 PM
My earlier post was a simplified version of my application so here is a more detailed account of that. In my application, I make an Ajax call (to the same server, so no cross-site scripting issue) like the following:


Ext.Ajax.request({
url : 'getNotificationsJson?0=100',
method : 'GET',
success : function(responseObject) {
var jsonContent = responseObject.responseText;
var json = Ext.decode(jsonContent, true);
messageCard.update(json);
},
failure : function() {
alert('Status','Unable to show records at this time. Please try again later.');
}
});The "messagCard" above is a component with a template:


var messageCard = new Ext.Component({
title: 'Messages',
iconCls: 'favorites',
cls: 'message',
scroll: 'vertical',
tpl: [
'<tpl for=".">',
'<h2>{source}</h2>',
'<p>{content}</p>',
'</tpl>'
]
});So the response to the ajax call is retrieved, the textual content is extracted and then I want to create a JS object from that to update the template. The textual content of the ajax response is what I had noted before and I have verified that it gets there correctly but when I try to decode that, it fails. I did test removing the "." in "T. Spencer" and still have the same issue.

My goal is to populate my template with the result returned from the ajax call so if there is a better way to do that, I am open to alternatives.

Many thanks
Ali.

evant
27 Jul 2010, 5:10 PM
It's not valid JSON, you need to quote the identifiers.

http://jsonlint.com/ is very useful