Example usage for org.json.simple JSONArray add

List of usage examples for org.json.simple JSONArray add

Introduction

In this page you can find the example usage for org.json.simple JSONArray add.

Prototype

public void add(int index, E element) 

Source Link

Document

Inserts the specified element at the specified position in this list.

Usage

From source file:geo.controller.LoadMRTServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w  w w . j a v a  2 s.  c om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    try {
        ArrayList<MRTStation> mrtList = MRTStationDAO.getMRTList();

        JSONArray jsonArray = new JSONArray();
        int index = 0;

        for (int i = 0; i < mrtList.size(); i++) {
            if (mrtList.get(i).lat == 0 && mrtList.get(i).lng == 0) {
                continue;
            }
            jsonArray.add(index++, mrtList.get(i).getJSONObject());
        }

        out.println(jsonArray.toJSONString());
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        out.close();
    }
}

From source file:geo.controller.LoadDataServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w w  w.  jav a2 s.com*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();

    try {
        ArrayList<Transaction> transactionList = TransactionDAO.getTransactionList();

        JSONArray jsonArray = new JSONArray();
        int index = 0;

        //hardcode to remove error in geocode
        for (int i = 0; i < transactionList.size(); i++) {
            if (transactionList.get(i).postalCode.equals("158744")) {
                continue;
            }
            jsonArray.add(index++, transactionList.get(i).getJSONObject());
        }

        out.println(jsonArray.toJSONString());

    } catch (Exception ex) {
        ex.printStackTrace();
    }

}