0

In my jsp, I'm trying to iterate over a JSONArray passed from js. This is what I did:

<script lang="javascript">
                    var movies = $("#searchResult");
                    console.log(movies);    
                    for (var i = 0; i < movies.length; i++) {
                        console.log(movies[i]);
                    }
</script>

but it does not give me anything.
Any help is appreciated!

June
  • 335
  • 4
  • 15
  • 1
    Can you please post your JSON array value which the `var movies` variable holds? – David R Mar 07 '17 at 06:26
  • Note: `#searchResult` will yield all elements with id `searchResult` and since id should be unique, that will give you an array of 1 element. – Rajesh Mar 07 '17 at 06:28
  • the `#` in `#searchResult` means that you will get a single element that has the `id` equal to `searchResult`. I recommend you to use tags instead. Something like this `$( "p" )` this expression will get the collection of DOM elements that have been selected from the document. In this case the tag `p`. – Teocci Mar 07 '17 at 06:43

4 Answers4

2

By "it does not give me anything", do you mean the 'movies' variable is undefined? If so, have you tried this answer? https://stackoverflow.com/a/4803931/6638533

Edit (addition): I think it would be more helpful to find the culprit by providing what the console.log method displays; so that we can know whether the problem is in the #searchResult or in the 'movies' variable's type.

Community
  • 1
  • 1
samAlvin
  • 1,648
  • 1
  • 11
  • 35
  • 2
    Though a good point and +1, this still falls under comment category and answers only linking to another post may attract downvotes. So you should try to justify as answer. – Rajesh Mar 07 '17 at 06:32
  • @Rajesh I don't think he has the privilege to comment on other people's questions yet... – Simon Mar 07 '17 at 06:36
  • @Simon I know that. It takes 50 rep to get comment privilege. My point is, his answer is like a comment and this might attract downvotes. But his point is valid. And he had no option but to put as answer, he should make it descriptive enough that it justifies for answer. – Rajesh Mar 07 '17 at 06:41
  • @Rajesh Thanks for the advice. I've edited my answer, does my edit helps? – samAlvin Mar 07 '17 at 06:54
1

It seems that you are assigning a JQuery element to movies, if this element is not there, you will generate a new one, then the movies.length will be 0, you won't get anything. If there is one element called searchResult, you will get an array of one element.

Simon
  • 629
  • 3
  • 8
1

The # in #searchResult means that you will get a single element that has the id equal to searchResult.

Solution No 1

I don't know where is your jsp I will assume that you have a getmovies.jsp request then you can receive the results like thisjQuery

$.getJSON("getmovies.jsp", function(json) {
    $.each (json, function(k, movie) {
        console.log(movie.title);
    });
}); 

Solution No. 2 If you have a code like this:

<% List moviesList = (List) session.getAttribute("clist");
JSONArray moviesJSON = new JSONArray();
JSONObject tmp;

for (int i = 0; i < selected.size(); i++) {
    tmp = new JSONObject();
    tmp.put("id", selected.get(i));
    tmp.put("title", selected.get(i).getTitle());

    moviesJSON.put(tmp);
}%>

Then you can receive the JSON in JavaScript like this:

var moviesJSON = <%=moviesJSON%>;
moviesJSON.each(function( movies ) {
movies.each(function(index, movie) {
    console.log( index + ": " + movie.title );
});

NOTE This is another alternative that you can try~

I recommend you to use tags instead. Something like this $( "li" ) this expression will get the collection of DOM elements that have been selected from the document. In this case the tag li.

This can be a solution for you.

var movies = $("#searchResult").find("li");
movies.each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="searchResult">
  These are the results:
  <ul class="level-1">
    <li class="result">I</li>
    <li class="result">II</li>
    <li class="result">III</li>
    <li class="result">IV</li>
    <li class="result">V</li>
  </ul>
</div>
Teocci
  • 7,189
  • 1
  • 50
  • 48
1

var movies = $("#searchResult"); console.log(movies);

The movies variable could be string. Please check it . If it is string the split it with separator then the loop will be ok.

Anupam Biswas
  • 66
  • 1
  • 4