Actionscript:
-
var str:String='{x: 10, y:10, width:100, heigth:100, name:"myClip", type:"clip"}';
-
-
var obj:Object=toObject(str);
-
-
trace(obj.x + obj.width, obj.name, obj.type);
-
-
function toObject(str:String):Object {
-
str=str.replace(/\{|\}/g,"");
-
// to an array of name value pairs [0] = name, [1] = value etc...
-
var pairs:Array=str.split(/\:|\,\s+/);
-
var obj:Object = new Object();
-
for (var i:int = 0; i<pairs.length; i+=2) {
-
obj[pairs[i]]=numOrString(pairs[i+1]);
-
}
-
return obj;
-
}
-
function numOrString(val:*):* {
-
if (isNaN(Number(val))) {
-
if (val.charAt(0) == '"' && val.charAt(val.length - 1) == '"'){
-
val = val.substr(1, val.length - 2);
-
}
-
} else {
-
val=Number(val);
-
}
-
return val;
-
}
A very limited way to parse an object from a string... Tomorrow I'll post a better version of this that's a good deal more complex and doesn't really use regular expressions... This works nicely if you don't have arrays as Object properties...
2 Comments
thx , i love this one , waiting for complex version, this already smaller than JSON, just missing Boolean type
thanks katopz… yeah this is pretty limited… booleans would be easy enough to add… just a conditional or two…