www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: converting a string to object literal object

  1. #1
    Join Date
    Mar 2007
    Posts
    21

    converting a string to object literal object

    Hi,
    I have an attribute on one of the input element like this
    validation = {min-date:'12/01/2007',max-date:'12/12/2007'}.
    i read the attribute value using
    var str = element.getAttribute('validation');
    I would like to access the min-date by saying str.min-date .
    This is possible if i can convert the string i got {min-date:'12/01/2007',max-date:'12/12/2007'} .This looks like a familiar string literal for objects.
    This is really challenging ..........

    THnks
    Blue

  2. #2
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    It is the basics of JSON:

    var foo = eval("(" + yourVariable + ")");

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  3. #3
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,667
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script language="JavaScript" type="text/javascript">
    /*<![CDATA[*/
    var validation = {min_date:'12/01/2007',max_date:'12/12/2007'};
    
    /*]]>*/
    </script></head>
    
    <body onload="document.getElementById('tst').val=validation;">
    <input id="tst" onclick="alert('min_date = '+this.val['min_date']+'\nmax_date = '+this.val['max_date']);"/>
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  4. #4
    Join Date
    Mar 2007
    Posts
    21
    I am not sure you guys understood my question.I have the code given below....Paste the code given below in your editor and Try solving it.I needed the first alert to work .

    <html>
    <head>
    <script>
    function init(){
    var ele = document.getElementById('first');
    var obj = ele.getAttribute('validation');
    // here the object has an object literal but this is a string object
    var foo = eval("(" + obj + ")");
    alert(obj.min-date);

    // I expect the behaviour like what is below
    obj = { date:'2/2007'};
    alert(obj.date);
    }
    </script>
    </head>
    <body >

    <div id='first' onclick='init()' validation= {min-date:'6/2006',max-date:'5/2007'}>
    test
    </div>

    </body>
    </html>

    THNks

  5. #5
    Join Date
    Jul 2005
    Location
    USA
    Posts
    3,910
    They did understand the question. We just don't always give the expected answer.

    1) Hyphens aren't allowed in variable names.

    2)
    Code:
    var foo = eval("(" + obj + ")");
    should be
    Code:
    obj = eval("(" + obj + ")");
    Learn CSS. | SSI | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

    Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

    Check out my blog.

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by cooolblue
    This is possible if i can convert the string i got {min-date:'12/01/2007',max-date:'12/12/2007'}
    No matter your conversion looks like an object. The value of any HTML (native or custom) attribute is always a string.

    There is a big difference between an HTML attribute of an HTML element and a property of an object.

    in:
    object.property
    the value assigned may be a number, a string, a sorted array or even another object/function/method

    while in
    <tagelement customAttribute="value" />
    the value assigned is always a string

    By the way, as Kravitz said, you should not use hyphens. Better, say, use underscore. And it is a good habbit to quote always the values of HTML(XHTML) attributes. In XHTML is required. Usually the double wuotes are preferred:
    Code:
    <div id="first" onclick="init()" validation= "{min_date:'6/2006',max_date:'5/2007'}">
    Last edited by Kor; 10-30-2007 at 04:04 AM.

  7. #7
    Join Date
    Mar 2007
    Posts
    21
    Got it resolved....sorry guys it was a lot of typo problem rather than the js....
    THnks Eric for the eval ....and THnks karvis for showing me the typo..

    Last edited by cooolblue; 10-30-2007 at 09:08 AM.

  8. #8
    Join Date
    Mar 2007
    Posts
    21
    Quote Originally Posted by cooolblue
    Got it resolved....sorry guys it was a lot of typo problem rather than the js....
    THnks Eric for the eval ....and THnks karvis for showing me the typo..



    Eric did you say JSON ......And you used eval and they it is costly...
    if so can you show in this example how to use just json..
    Last edited by cooolblue; 10-30-2007 at 09:29 AM.

  9. #9
    Join Date
    Feb 2006
    Posts
    2,927
    If you don't want to use eval you can interpret the string yourself-

    Code:
    var str= "min_date:6/2006,max_date:5/2007"
    
    function avoidEvalAndGetObjectfromString(str){
      var V={};
      var A=str.split(',');
      var L= A.length,temp;
      for(var i=0; i<L; i++){
          temp= A[i].split(':');
          V[temp[0]]=temp[1];
      }
      return V;
    }

  10. #10
    Join Date
    Mar 2007
    Posts
    21
    associate arrays .YEP .Good one...But just wondering if I have to do this...or there is already a js which would do this in case of complex object literals...
    THnks for you help.

  11. #11
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,355

    Red face Syntax question.

    I was trying out post #9, but seem to be getting nothing.
    What am I doing wrong with the following test?
    PHP Code:
    <html>
    <
    head>
    <
    title>JSON test</title>
    <
    script type="text/javascript">
    var 
    str "min_date:6/2006,max_date:5/2007"

    // function avoidEvalAndGetObjectfromString(str){
    function avoidEvalAndGetObjectfromString(){
      var 
    V={};
      var 
    A=str.split(',');
      var 
    LA.length,temp;
      for(var 
    i=0i<Li++){
          
    tempA[i].split(':');
          
    V[temp[0]]=temp[1];
      }
      return 
    V;
    }
    </script>
    </head>
    <body>
    <button onClick="alert(avoidEvalAndGetObjectfromString())">Test1</button>
    <!-- button onClick="alert(avoidEvalAndGetObjectfromString(str))">Test2</button -->
    </body>
    </html> 
    I've tried it with and without passing the variable 'str' in the 'alert' with same results.
    I'm just getting the reply of [object Object] in the alert box.
    Obviously I'm doing something goofy!

  12. #12
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    You are getting an object in the alert because it is returning an object.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  13. #13
    Join Date
    Mar 2007
    Posts
    21

    Unhappy hey eric

    you didn't answer my question.Somebody hijacked my question.
    Do you know of any js which will do the work of splitting the string to object literal for me in case of lengthy and complex object literal with arrays etc..
    THnks

  14. #14
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    Is there a reason why you will not use eval to do it?

    Code:
    var myString = "{foo:'bar',asdf=[1,2,3,4],color:{apple:'red'}}";
    var myObj = eval("(" + myString + ")");
    for(x in myObj){
      alert(x + ":" + myObj[x]);
    }
    If you want to loop through, you can look at the link at the bottom of this page: http://www.json.org/js.html

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint] | twitter | linkedin | http://www.pascarello.com

  15. #15
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by JMRKER
    I was trying out post #9, but seem to be getting nothing.
    What am I doing wrong with the following test?
    PHP Code:
    <html>
    <
    head>
    <
    title>JSON test</title>
    <
    script type="text/javascript">
    var 
    str "min_date:6/2006,max_date:5/2007"

    // function avoidEvalAndGetObjectfromString(str){
    function avoidEvalAndGetObjectfromString(){
      var 
    V={};
      var 
    A=str.split(',');
      var 
    LA.length,temp;
      for(var 
    i=0i<Li++){
          
    tempA[i].split(':');
          
    V[temp[0]]=temp[1];
      }
      return 
    V;
    }
    </script>
    </head>
    <body>
    <button onClick="alert(avoidEvalAndGetObjectfromString())">Test1</button>
    <!-- button onClick="alert(avoidEvalAndGetObjectfromString(str))">Test2</button -->
    </body>
    </html> 
    I've tried it with and without passing the variable 'str' in the 'alert' with same results.
    I'm just getting the reply of [object Object] in the alert box.
    Obviously I'm doing something goofy!
    Of course You returned the object new created, what did you expect else? You want a certain value?
    Code:
    ...
      return V['min_date'];
    ...

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