Solved

Looping JSON data

Posted on 2009-06-29
7
542 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now