Solved

Looping JSON data

Posted on 2009-06-29
7
545 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
  • 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

808 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