www.webdeveloper.com
Results 1 to 6 of 6

Thread: String to Object

  1. #1
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    75

    String to Object

    Is it possible or is there something already out there that can turn a string into an object?

    Lets say for example, you have this string: "content:uniqueDiv|width:300|height:150"

    Now is it possible to create a function or something to dynamically create an object for you using this string. So the text in bold is the property and the text in italics is the property's value.

    So using this example the object you would return is:
    {content: 'uniqueDiv', width: 300, height: 150}

    Any help would be really appreciated. Thanks!

  2. #2
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    You'll have more luck finding a routine that can serialize/deserialize objects safely if you use a well-established format like JSON or XML.

    But, for the format you've provided, here's a really simple approach, which assumes the keys and values are URI encoded:

    PHP Code:
    var deserialize = function(s) {
      var 
    rv = {};
      var 
    pairs s.split("|");
      for (var 
    i in pairs) {
        var 
    pairs[i].split(":");
        if (
    p.length == 2) {
          var 
    decodeURIComponent(p[0]);
          var 
    decodeURIComponent(p[1]);
          
    rv[k] = v;
        }
      }
      return 
    rv;
    // deserialize() 
    Using the above method, if a key is specified twice, the 2nd value takes precedence. And nested objects are not supported.
    Last edited by svidgen; 05-18-2012 at 10:53 AM. Reason: foreach => for ... languages are starting to blend together in my brain ...
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  3. #3
    Join Date
    Nov 2010
    Posts
    1,082
    will turn the numbers into strings, but you can deal with that later if need be:

    Code:
    <script type="text/javascript">
    str="content:uniqueDiv|width:300|height:150"
    atts=str.split("|")
    myObj={}
    for (var i = 0; i < atts.length; i++) {
    myObj[atts[i].split(":")[0]]=atts[i].split(":")[1];
    }
    
    </script>

  4. #4
    Join Date
    Nov 2010
    Posts
    1,082
    svidgen: seems to me that yours works better with this line instead:
    Code:
    var p = pairs[i].split(":");

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    Quote Originally Posted by xelawho View Post
    svidgen: seems to me that yours works better with this line instead:
    Code:
    var p = pairs[i].split(":");
    Good catch. I've fixed it above.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  6. #6
    Join Date
    Aug 2008
    Location
    NYC
    Posts
    75
    Wow! Thank you for all the responses. I was kind of stumped by this. Thanks for all your help! I really really appreciate it.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles