?
Solved

Looping JSON data

Posted on 2009-06-29
7
Medium Priority
?
548 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

762 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