0

I have 2 set of data in my jsp page , one is a array list and another is json data . Now i want to parse both the data set and create my own drop downs .

Here is the sample Array List named json_obj data

["ABC-1","ABC-2","ABC-3","ABC-4","ABC-5","ABC-6"]

I tried this piece of code but not working

<select>
  <option value="all_qns">All</option>
  <c:forEach var="strategy" items="${json_obj}" varStatus="strategyLoop">
    <option><c:out value="${strategyLoop[index]}"/></option>

  </c:forEach>
</select>

Getting blank options

<select>
 <option value="all_qns">All</option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
 <option></option>
</select>

Also I do have this piece of json data named json_obj_m

{"a":"1050","b":"1079","c":"1073","d":"1074"}

And I a=have tried this :

<c:forEach items="${json_obj_m}" var="met">
 <option value="${met.key}">${met.value}</option>
</c:forEach>

But not working again getting error , that spring does not support key .

Can anybody guide me where I am doing mistake, very new to Java/Spring . Thanks in advance.

4
  • You need to construct java object from JSON before applying JSTL tags. Commented Jul 15, 2014 at 14:17
  • Ok so can guide me a bit how to do it. Commented Jul 15, 2014 at 14:19
  • If you want to try "javascript" approach, you may refer this:stackoverflow.com/questions/20756970/… Commented Jul 15, 2014 at 14:21
  • If you want to try "java" approach, it is better to do it in controller and send it, example: stackoverflow.com/questions/1688099/converting-json-to-java Commented Jul 15, 2014 at 14:22

2 Answers 2

1

Your usage of JSTP foreach is incorrect : you get the value in strategy and try to use (badly) strategyLoop which is the status. You should write simply :

<option>${strategy}</option>

The status helps to count the iterations and you use ${strategyLoop.index} or ${strategyLoop.count}:

  • strategyLoop.index starts at 0
  • strategyLoop.count starts at 1
Sign up to request clarification or add additional context in comments.

Comments

1

For iterating over list use this code:

<select id="someId">
  <option value="all_qns">All</option>
  <c:forEach var="strategy" items="${json_obj}" >
    <option value="${strategy}">${strategy}</option>

  </c:forEach>
</select>

And if you are getting JSON through Ajax call, you can use this (By JavaScript):

$.each(data, function(key, value) { 
         $('#someId').append("<option value="+key+"option>"+value+"</option>");  
    });
});

1 Comment

JSON data works fine , But when I am doing the same for ArrayList it is not working . I made it working by Javascript as : dataMatel = <%=request.getAttribute("json_obj")%>; $.each(dataMatel, function(value) { $('#questionID').append("<option value="+dataMatel[value]+">"+dataMatel[value]+"</option>"); }); But still I am not sure how to iterate in JSP page(not using Javascript) . I tried this <c:forEach var="strategy" items="${json_obj}" > <h1>${strategy}</h1> </c:forEach> But Output is like `["ABC-1" "ABC-2" "ABC-3"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.