PDA

View Full Version : Ext.decode does not work



lakilevi
14 Oct 2011, 2:31 AM
Hello.

I am kind new in Touch, but I have some experience in ExtJs.

I was trying to use the Ext.decode or Ext.utils.JSON.decode as in following example



var stringData = "{date: 'Friday 03rd of August 2007 02:47:09 PM'}";
try {
var jsonData = Ext.decode(stringData);
alert('Success');
// You can do anything with jsonData if you want.
}
catch (err) {
alert('ERROR');
}


But I got always the Error message, so there might be some problem with the decode function?

Thank you very much!

t.hoepfner
14 Oct 2011, 5:27 AM
Ext.decode decodes JSON. JSON requires double-quotes around the keys and string values. Just use

var stringData = '{"date": "Friday 03rd of August 2007 02:47:09 PM"}';
instead, then it should work.

Also see the JSON article on Wikipedia (http://en.wikipedia.org/wiki/JSON).

HTH,

Timo

AndreaCammarata
14 Oct 2011, 5:28 AM
Hi lakilevi,
that's because your is not a valid JSON string.
Try this



var a;
Ext.setup({
onReady: function() {

var stringData = '{"date": "Friday 03rd of August 2007 02:47:09 PM"}';
try {
var jsonData = Ext.decode(stringData);
alert('Success');
console.log(jsonData);
// You can do anything with jsonData if you want.
}
catch (err) {
alert('ERROR');
}

}

});


In these cases, I always suggest to validate the JSON using JSONLint (http://jsonlint.com/).

Hope this helps.

lakilevi
17 Oct 2011, 12:54 AM
Thank you very much!
You are right, it is working now.

But anyway, it is wired, becuase in case of ExtJs 1.x-3.x I made lot of jason decodes without any double-quotes. All my ajax requestes returned jason encoded data, without quotes, and in extjs it was decoded with success.

Thank you very much again!