List of usage examples for org.json JSONWriter object
public JSONWriter object() throws JSONException
From source file:com.vaynberg.wicket.select2.AbstractSelect2Choice.java
@Override public void onResourceRequested() { // this is the callback that retrieves matching choices used to populate the dropdown Request request = getRequestCycle().getRequest(); IRequestParameters params = request.getRequestParameters(); // retrieve choices matching the search term String term = params.getParameterValue("term").toOptionalString(); int page = params.getParameterValue("page").toInt(1); // select2 uses 1-based paging, but in wicket world we are used to // 0-based/* ww w.j av a 2 s . c o m*/ page -= 1; Response<T> response = new Response<T>(); provider.query(term, page, response); // jsonize and write out the choices to the response WebResponse webResponse = (WebResponse) getRequestCycle().getResponse(); webResponse.setContentType("application/json"); OutputStreamWriter out = new OutputStreamWriter(webResponse.getOutputStream(), getRequest().getCharset()); JSONWriter json = new JSONWriter(out); try { json.object(); json.key("results").array(); for (T item : response) { json.object(); provider.toJson(item, json); json.endObject(); } json.endArray(); json.key("more").value(response.getHasMore()).endObject(); } catch (JSONException e) { throw new RuntimeException("Could not write Json response", e); } try { out.flush(); } catch (IOException e) { throw new RuntimeException("Could not write Json to servlet response", e); } }