PDA

View Full Version : How can we let the property name without quotation marks?



bywayboy
16 Aug 2009, 9:42 PM
when i use Ext.encode()

the property name in quotation marks. How can we let the property name without quotation marks?

Lukman
17 Aug 2009, 6:07 AM
Why? A proper JSON-encoded string that represents an object must have the property names quoted in quotation marks: http://www.json.org/

If you want to print out an object as string, then maybe this little function can help:

function objToString(obj, lpad, noindent1st) {
if (!lpad) { lpad = ''; }
if (!Ext.isObject(obj)) {
var nobj = Ext.isString(obj)? "'" + String.escape(obj) + "'" : obj;
return noindent1st? nobj : lpad + nobj;
}
var clpad = noindent1st? lpad : lpad + '\t';
if (Ext.isArray(obj)) {
var out = noindent1st? '[\n' : lpad + '[\n';
for (var i = 0; i < obj.length;) {
out += objToString(obj[i], clpad);
i++;
if (i < obj.length) {
out += ',';
}
out += '\n';
}
out += (noindent1st? lpad.substr(0, lpad.length - 1) : lpad) + ']';
}
else {
var out = noindent1st? '{\n' : lpad + '{\n';
var keys = [];
for (var k in obj) {
keys.push(k);
}
for (var i = 0; i < keys.length;) {
out += clpad + keys[i] + ': ' + objToString(obj[keys[i]], clpad + '\t', true);
i++;
if (i < keys.length) {
out += ',';
}
out += '\n';
}
out += (noindent1st? lpad.substr(0, lpad.length - 1) : lpad) + '}';
}
return out;
}Use it like this:

var obj_string = objToString(obj);It fully indents the object's children and all. And the output string is fully compatible with Ext.decode(). The only thing it lacks is it doesn't support displaying functions, which Ext.encode() strips out anyway.

bywayboy
26 Aug 2009, 10:17 AM
thank you very much!