{"id":263,"date":"2009-04-23T10:48:30","date_gmt":"2009-04-23T17:48:30","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=1216"},"modified":"2009-04-23T10:48:30","modified_gmt":"2009-04-23T17:48:30","slug":"object-from-string","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=263","title":{"rendered":"Object from String"},"content":{"rendered":"<pre class=\"brush: as3; title: ; notranslate\" title=\"\">\r\nvar str:String=&#039;{x: 10, y:10, width:100, heigth:100, name:&quot;myClip&quot;, type:&quot;clip&quot;}&#039;;\r\n\r\nvar obj:Object=toObject(str);\r\n\r\ntrace(obj.x + obj.width, obj.name, obj.type);\r\n\r\nfunction toObject(str:String):Object {\r\n\tstr=str.replace(\/{|}\/g,&quot;&quot;);\r\n\t\/\/ to an array of name value pairs &#x5B;0] = name, &#x5B;1] = value etc...\r\n\tvar pairs:Array=str.split(\/:|,s+\/);\r\n\tvar obj:Object = new Object();\r\n\tfor (var i:int = 0; i&lt;pairs.length; i+=2) {\r\n\t\tobj&#x5B;pairs&#x5B;i]]=numOrString(pairs&#x5B;i+1]);\r\n\t}\r\n\treturn obj;\r\n}\r\nfunction numOrString(val:*):* {\r\n\tif (isNaN(Number(val))) {\r\n\t\tif (val.charAt(0) == &#039;&quot;&#039; &amp;&amp; val.charAt(val.length - 1) == &#039;&quot;&#039;){\r\n\t\t\tval = val.substr(1, val.length - 2);\r\n\t\t}\r\n\t} else {\r\n\t\tval=Number(val);\r\n\t}\r\n\treturn val;\r\n}\r\n<\/pre>\n<p>A very limited way to parse an object from a string&#8230; Tomorrow I&#8217;ll post a better version of this that&#8217;s a good deal more complex and doesn&#8217;t really use regular expressions&#8230; This works nicely if you don&#8217;t have arrays as Object properties&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var str:String=&#039;{x: 10, y:10, width:100, heigth:100, name:&quot;myClip&quot;, type:&quot;clip&quot;}&#039;; var obj:Object=toObject(str); trace(obj.x + obj.width, obj.name, obj.type); function toObject(str:String):Object { str=str.replace(\/{|}\/g,&quot;&quot;); \/\/ to an array of name value pairs &#x5B;0] = name, &#x5B;1] = value etc&#8230; var pairs:Array=str.split(\/:|,s+\/); var obj:Object = new Object(); for (var i:int = 0; i&lt;pairs.length; i+=2) { obj&#x5B;pairs&#x5B;i]]=numOrString(pairs&#x5B;i+1]); } return obj; } function [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,20,21],"tags":[3,4],"class_list":["post-263","post","type-post","status-publish","format-standard","hentry","category-dynamic","category-string-manipulation","category-strings","tag-actionscript","tag-flash"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=263"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/263\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}