473,382 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

javascript eval problem

mickey0
142 100+
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
14 2935
gits
5,390 Expert Mod 4TB
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
142 100+
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
5,390 Expert Mod 4TB
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
142 100+
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
5,390 Expert Mod 4TB
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
142 100+
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
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
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
5,390 Expert Mod 4TB
that is correct :)

kind regards
Apr 4 '08 #14
That sure is helps.

thanks alot
Apr 4 '08 #15

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

Similar topics

4
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following...
7
by: Marci | last post by:
I found this script for cascading menus, however, I cannot reach the author to solve the bug I am having when I add a second menu to it. My problem is this: If I click on the first link, the...
4
by: binnyva | last post by:
Hello Everybody, I am writing an interactive tutorial for JavaScript. I created a text box into which the users can input javascript commands. On pressing a button, these commands will be...
3
by: jimmygoogle | last post by:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can...
8
by: | last post by:
The problem lies here eval("document.TeeForm.amt.value(S+M)"); S and M suppose to add up and the total suppose to appear on the AMT field but it didn't. Any help? ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.