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.

curiousguy
  • 3,212
  • 8
  • 39
  • 71
  • You need to construct java object from JSON before applying JSTL tags. – kosa Jul 15 '14 at 14:17
  • Ok so can guide me a bit how to do it. – curiousguy Jul 15 '14 at 14:19
  • If you want to try "javascript" approach, you may refer this:http://stackoverflow.com/questions/20756970/how-to-display-json-object-in-jsp – kosa Jul 15 '14 at 14:21
  • If you want to try "java" approach, it is better to do it in controller and send it, example: http://stackoverflow.com/questions/1688099/converting-json-to-java – kosa Jul 15 '14 at 14:22

2 Answers2

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
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
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>");  
    });
});
Shailesh Saxena
  • 3,472
  • 2
  • 18
  • 28
  • `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(""); });` But still I am not sure how to iterate in JSP page(not using Javascript) . I tried this `

    ${strategy}

    ` But Output is like `["ABC-1" "ABC-2" "ABC-3"
    – curiousguy Jul 17 '14 at 06:54