Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Looping JSON data

Posted on 2009-06-29
7
Medium Priority
?
551 Views
Last Modified: 2013-11-08
Dear Experts,

I have the following javascript which does not work properly

objJSON = eval('('+ results +')');

for(var i = 0, e = objJSON.selectbox1.length; i < e; ++i) {
    var option = new Option(objJSON.selectbox1[i].value, objJSON.selectbox1[i].text);
    document.getElementById('selItemType').options[i] = option;
}

It should be populating my html select control, but it just fills it up with "undefined" values.

The json data looks like this

{
"textbox1" : "value1",
"textbox2" : "value2",
"selectbox1" : [
{ "value" : "1", "text" : "one" },
{ "value" : "2", "text" : "two" },
{ "value" : "3", "text" : "three" }
]
}

So basically, I want to use the selectbox1 part to loop through the arrays and populate the select box.

Please help
0
Comment
Question by:narmi2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 18

Expert Comment

by:Morcalavin
ID: 24735736
This is a duplicate question.  You already have this question open here: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/JSON/Q_24517811.html

Please close one or the other.
0
 
LVL 1

Author Comment

by:narmi2
ID: 24735753
This is a different problem, the problem there was that the loop would not loop which i have not fixed, however, this question the loop will loop, but fills the select box will undefined values.
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 24735758
The below works fine for me.  Where is 'results' variable coming from?  Is it from an ajax request?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
</head>
<body>
<select id="names"><option>Default option if JS is disabled</option></select>
<script type="text/javascript">
 
var mySelectBox = document.getElementById('names');
mySelectBox.options.length = 0;
var myData = {"textbox1" : "value1","textbox2" : "value2","selectbox1" : [{ "value" : "1", "text" : "one" },{ "value" : "2", "text" : "two" },{ "value" : "3", "text" : "three" }]}; //this only needs to be eval'ed if it's a string
for(var i = 0, e = myData.selectbox1.length; i < e; ++i) {
    var option = new Option(myData.selectbox1[i].value, myData.selectbox1[i].text);
    mySelectBox.options[i] = option;
}
</script>
</body>
</html>

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:narmi2
ID: 24735773
Yes, the "results" in

objJSON = eval('('+ results +')');

is from an AJAX request which has the value

{
"textbox1" : "value1",
"textbox2" : "value2",
"selectbox1" : [
{ "value" : "1", "text" : "one" },
{ "value" : "2", "text" : "two" },
{ "value" : "3", "text" : "three" }
]
}

So when I eval it and try to loop it into a select box, it gives me lots of undefined values.
0
 
LVL 18

Expert Comment

by:Morcalavin
ID: 24735810
Does the ajax data contain those line returns?  If so, you may need to remove the new line characters(start with \n).  I think all the code is working fine.  I think your JSON isn't formatted right and it is being eval'd improperly.

Look here:
http://stackoverflow.com/questions/395379/problem-when-retrieving-text-in-json-format-containing-line-breaks-with-jquery
0
 
LVL 18

Accepted Solution

by:
Morcalavin earned 2000 total points
ID: 24735827
Hmm..my mad.  You aren't using jquery.  Anyway, the idea should be the same.  Whatever server side script you are using should be replacing the line returns with something javascript can eval properly.
0
 
LVL 29

Expert Comment

by:Badotz
ID: 24735902
For all things JSON, visit http://www.json.org
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question