javascript eval problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    javascript eval problem

    Hello, I'm trying to convert a json object into a javascript object but when I write (in my file .html) the line relative to 'eval' the page doen't work and don't display anything:
    Code:
    <html>
    <body>
    <script type="text/javascript">
    var myJSON = {"count":26,.......};
    var myObject = eval("("+ myJSON+")");   //problem here
    document.writeln(myJSON.value.title);
     </script>
    </body>
    </html>
    If I comment the 'eval' line, writeln print properly; otherwise it doesn't print!
    Why eval doens't work, please?

    Thanks.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    hi ...

    welcome to theScripts ...

    i assume you provided that code as an example ... since your myJSON already is an JS-object it has no need to be evaled ... and i think your string comes from a XMLHttpRequest. Have a look at the following example:

    [CODE=javascript]
    var myJSON = '[{"count": "26"}]';
    var myObject = eval(myJSON);

    alert(myObject[0].count);
    [/CODE]
    in case you want to eval the input has to be a string (myJSON-variable) ... and it has to be an Array (a list of records - aka. javascript-objects) even when there is only one element

    kind regards

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      Hi,
      and thanks for fast reply.
      I'm a bit confused. Yes I have my JSON object yet:
      var myJSON = {"count":26,"va lue": "fdfdfddf";.... ............... ..............} ;
      and what I'm trying to do is convert that variable to a Javascript object (to manipulate it but at the moment I don't know how manipulate). I'm doing practice....
      How can I convert it properly, please?
      Thanks.

      EDIT: is there a way, with a Javascript object, to to do a loop on it and print all "value" content fields? (I have many filelds labeled "value" in the JSon).

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        [CODE=javascript]var myJSON = {"count":26,"va lue": "fdfdfddf";.... ............... ..............} ;[/CODE]

        as i said ... this is a JavaScript-Object already ... you don't need to eval that! ... have a look at the following example:

        [CODE=javascript]// create a objectinstance
        // 1. with constructor
        var o_1 = new Object;

        // 2. second - we use literals ... this is equivalent to
        // option 1. above
        var o_2 = {};

        // so o_1 and o_2 are JavaScript-Objects now :)

        // now lets loop:
        var o_3 = {
        foo: 1,
        bar: 2,
        fbr: 'test'
        };

        for (var i in o_3) {
        // i is the key (name of the property and o_3[i] is its value
        // so we alert prop and value here (all)
        alert('key ' + i + ' in obj o_3 has value: ' + o_3[i]);
        }
        [/CODE]
        kind regards

        Comment

        • mickey0
          New Member
          • Jan 2008
          • 142

          #5
          Originally posted by gits
          [CODE=javascript]
          for (var i in o_3) {
          // i is the key (name of the property and o_3[i] is its value
          // so we alert prop and value here (all)
          alert('key ' + i + ' in obj o_3 has value: ' + o_3[i]);
          }
          [/CODE]
          OK, but How can I print the name of 'key' instead of its content? (for example: o_3[0] is 'foo').

          Thanks.

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            as you see in the example, when you loop with:

            [CODE=javascript]for (var i in obj) { // i is the key :) }[/CODE]
            i is the key (name and not an index) ... simply print it the way you want ...

            kind regards

            Comment

            • mickey0
              New Member
              • Jan 2008
              • 142

              #7
              Hi, my problem is a bit different and itI doesn't work; see this:
              Code:
              var o_3 = [
                  { foo: 1, bar: 2, fbr: 'test'}, { foo: 99, bar: 100, fbr: 'notest'}        
              ];    
              for (var i in o_3) {
                alert('key ' + i + ' in obj o_3 has value: ' + o_3[i].foo);
              }
              It print as key, 0,1 but I'd like "foo", "foo"

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                you silently changed your code ;) ... so now in your case you don't have an object ... but an array (array of objects in your case)! ... so you have to do the following:

                [CODE=javascript]var o_3 = [
                { foo: 1, bar: 2, fbr: 'test' }, { foo: 99, bar: 100, fbr: 'notest' }
                ];

                // loop through the array
                for (var i = 0, item; item = o_3[i], i++) {
                // loop through the current element which is an object in our case
                for (var j in item) {
                alert('key ' + j + ' of item ' + i + ' in array o_3 has value: ' + item[j]);
                }
                }[/CODE]
                kind regards

                Comment

                • komaruloh
                  New Member
                  • Mar 2008
                  • 4

                  #9
                  Sorry to drop in, but I really have to ask. When I eval() a JSON object in a text form, it goes error.
                  Code:
                   
                  var jsonText = "{"name":"json"}"; 
                  var jsonObj = eval(jsonText);
                  I've found out that I can do this instead :
                  Code:
                   
                  var jsonText = "{"name":"json"}"; 
                  var jsonObj = eval('('+jsonText+')');
                  that I have to add parenthesis to jsonText. However this is not needed for jsonText that represent array, and object in array.

                  Does anyone can explain me?

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    since eval could evaluate every string including functions and loops, if-statements etc. you have to mark your 'object literally string' with extra parantheses for the js-interpreter as not to be any block or statement that could belong to an if statement for example ... the only way that the interpreter could know that would be to look for an equals sign before curly braces or parantheses around it ... to mark that it is an expression rather then a statement ...

                    kind regards

                    Comment

                    • komaruloh
                      New Member
                      • Mar 2008
                      • 4

                      #11
                      Originally posted by gits
                      since eval could evaluate every string including functions and loops, if-statements etc. you have to mark your 'object literally string' with extra parantheses for the js-interpreter as not to be any block or statement that could belong to an if statement for example ... the only way that the interpreter could know that would be to look for an equals sign before curly braces or parantheses around it ... to mark that it is an expression rather then a statement ...

                      kind regards
                      Thanks for the quick reply.
                      Correct me if I am wrong, are you saying that it is because the curly braces rather than the JSON. I meant because it contains a curly brace in the first character, the js interpreter (could) be misunderstood.

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        i don't know exactly what you mean ... but the interpeter cannot know that you put a JSON-string in the eval ... so you have to tell the interpreter that it is one ... so that the interpreter may look for the parantheses and know that the text inside is to be evaled as an expression ...

                        kind regards

                        Comment

                        • komaruloh
                          New Member
                          • Mar 2008
                          • 4

                          #13
                          So the point is to use parentheses to let the js interpreter know that it is an expression. That way it can interpret it correctly, Is that so?

                          thanks for sharing your knowledge.

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            that is correct :)

                            kind regards

                            Comment

                            • komaruloh
                              New Member
                              • Mar 2008
                              • 4

                              #15
                              That sure is helps.

                              thanks alot

                              Comment

                              Working...