By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,089 Members | 2,191 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,089 IT Pros & Developers. It's quick & easy.

javascript eval problem

mickey0
100+
P: 142
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:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <script type="text/javascript">
  4. var myJSON = {"count":26,.......};
  5. var myObject = eval("("+ myJSON+")");   //problem here
  6. document.writeln(myJSON.value.title);
  7.  </script>
  8. </body>
  9. </html>
  10.  
If I comment the 'eval' line, writeln print properly; otherwise it doesn't print!
Why eval doens't work, please?

Thanks.
Jan 17 '08 #1
Share this Question
Share on Google+
14 Replies


gits
Expert Mod 5K+
P: 5,331
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:

Expand|Select|Wrap|Line Numbers
  1. var myJSON = '[{"count": "26"}]';
  2. var myObject = eval(myJSON);
  3.  
  4. alert(myObject[0].count);
  5.  
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
Jan 17 '08 #2

mickey0
100+
P: 142
Hi,
and thanks for fast reply.
I'm a bit confused. Yes I have my JSON object yet:
var myJSON = {"count":26,"value": "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).
Jan 17 '08 #3

gits
Expert Mod 5K+
P: 5,331
Expand|Select|Wrap|Line Numbers
  1. var myJSON = {"count":26,"value": "fdfdfddf";.................................};
as i said ... this is a JavaScript-Object already ... you don't need to eval that! ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. // create a objectinstance
  2. // 1. with constructor
  3. var o_1 = new Object;
  4.  
  5. // 2. second - we use literals ... this is equivalent to
  6. // option 1. above
  7. var o_2 = {};
  8.  
  9. // so o_1 and o_2 are JavaScript-Objects now :)
  10.  
  11. // now lets loop:
  12. var o_3 = {
  13.     foo: 1,
  14.     bar: 2,
  15.     fbr: 'test'
  16. };
  17.  
  18. for (var i in o_3) {
  19.     // i is the key (name of the property and o_3[i] is its value
  20.     // so we alert prop and value here (all)
  21.     alert('key ' + i + ' in obj o_3 has value: ' + o_3[i]);
  22. }
  23.  
kind regards
Jan 18 '08 #4

mickey0
100+
P: 142
Expand|Select|Wrap|Line Numbers
  1. for (var i in o_3) {
  2.     // i is the key (name of the property and o_3[i] is its value
  3.     // so we alert prop and value here (all)
  4.     alert('key ' + i + ' in obj o_3 has value: ' + o_3[i]);
  5. }
  6.  
OK, but How can I print the name of 'key' instead of its content? (for example: o_3[0] is 'foo').

Thanks.
Jan 18 '08 #5

gits
Expert Mod 5K+
P: 5,331
as you see in the example, when you loop with:

Expand|Select|Wrap|Line Numbers
  1. for (var i in obj) { // i is the key :) }
i is the key (name and not an index) ... simply print it the way you want ...

kind regards
Jan 18 '08 #6

mickey0
100+
P: 142
Hi, my problem is a bit different and itI doesn't work; see this:
Expand|Select|Wrap|Line Numbers
  1. var o_3 = [
  2.     { foo: 1, bar: 2, fbr: 'test'}, { foo: 99, bar: 100, fbr: 'notest'}        
  3. ];    
  4. for (var i in o_3) {
  5.   alert('key ' + i + ' in obj o_3 has value: ' + o_3[i].foo);
  6. }
  7. It print as key, 0,1 but I'd like "foo", "foo"
  8.  
Jan 18 '08 #7

gits
Expert Mod 5K+
P: 5,331
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:

Expand|Select|Wrap|Line Numbers
  1. var o_3 = [
  2.     { foo: 1, bar: 2, fbr: 'test' }, { foo: 99, bar: 100, fbr: 'notest' }       
  3. ];  
  4.  
  5. // loop through the array
  6. for (var i = 0, item; item = o_3[i], i++) {
  7.     // loop through the current element which is an object in our case
  8.     for (var j in item) {
  9.         alert('key ' + j + ' of item ' + i + ' in array o_3 has value: ' + item[j]);
  10.     }
  11. }
kind regards
Jan 18 '08 #8

P: 4
Sorry to drop in, but I really have to ask. When I eval() a JSON object in a text form, it goes error.
Expand|Select|Wrap|Line Numbers
  1.  
  2. var jsonText = "{"name":"json"}"; 
  3. var jsonObj = eval(jsonText);
  4.  
I've found out that I can do this instead :
Expand|Select|Wrap|Line Numbers
  1.  
  2. var jsonText = "{"name":"json"}"; 
  3. var jsonObj = eval('('+jsonText+')');
  4.  
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?
Mar 28 '08 #9

gits
Expert Mod 5K+
P: 5,331
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
Mar 28 '08 #10

P: 4
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.
Apr 3 '08 #11

gits
Expert Mod 5K+
P: 5,331
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
Apr 3 '08 #12

P: 4
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.
Apr 4 '08 #13

gits
Expert Mod 5K+
P: 5,331
that is correct :)

kind regards
Apr 4 '08 #14

P: 4
That sure is helps.

thanks alot
Apr 4 '08 #15

Post your reply

Sign in to post your reply or Sign up for a free account.