Example usage for org.json.simple JSONArray JSONArray

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

Introduction

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

Prototype

JSONArray

Source Link

Usage

From source file:com.aerothai.database.model.ModelService.java

public JSONObject GetModelAll(int idunit, String opt) throws Exception {

    Connection dbConn = null;//from w w  w .ja  va  2 s .  c o  m
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM model";
        if (idunit > 0) {
            query = query + " WHERE idunit =" + idunit;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM model WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idmodel"));
            jsonData.put("details", rs.getString("details"));
            jsonData.put("idunit", rs.getString("idunit"));
            jsonData.put("iddevice_type", rs.getString("iddevice_type"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:com.imagelake.android.category.Servlet_categories.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    PrintWriter out = response.getWriter();

    CategoriesDAOImp categoryDAOImp = new CategoriesDAOImp();
    JSONArray ja = new JSONArray();
    ArrayList<Categories> categoriesList = (ArrayList<Categories>) categoryDAOImp.listAllCategories();
    if (!categoriesList.isEmpty()) {
        for (Categories cc : categoriesList) {
            JSONObject jo = new JSONObject();
            jo.put("id", cc.getCategory_id());
            jo.put("cat", cc.getCategory());
            ja.add(jo);/*from   w w w.  j ava 2  s . c  o  m*/

        }
        out.write("json=" + ja.toJSONString());
    } else {

        JSONObject jo = new JSONObject();
        jo.put("id", 0);
        jo.put("cat", "No category found.");
        ja.add(jo);
        out.write("json=" + ja.toJSONString());

    }
}

From source file:com.romb.hashfon.helper.Helper.java

public String getJson(List list) {
    JSONArray jsonList = new JSONArray();
    for (Object o : list) {
        JSONObject obj1 = new JSONObject();
        for (Field field : o.getClass().getDeclaredFields()) {
            if (!field.getName().equals("serialVersionUID")) {
                field.setAccessible(true);
                String s = "";
                try {
                    s = field.get(o) + "";
                } catch (IllegalArgumentException | IllegalAccessException ex) {
                    Logger.getLogger(Helper.class.getName()).log(Level.SEVERE, null, ex);
                }//from w  ww  .  j  a  va2  s.  co m
                obj1.put(field.getName(), s);
            }
        }
        jsonList.add(obj1);
    }
    return jsonList.toJSONString();
}

From source file:com.aerothai.database.unit.UnitService.java

public JSONObject GetUnitAll(String action, String opt) throws Exception {

    Connection dbConn = null;/* w w  w .j a va  2 s .  co  m*/
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM unit";
        if (!action.equals("0")) {
            query = query + " WHERE action =" + action;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM unit WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idunit"));
            jsonData.put("name", rs.getString("name"));
            jsonData.put("shortname", rs.getString("shortname"));
            jsonData.put("action", rs.getString("action"));
            jsonData.put("addjob", rs.getString("addjob"));
            jsonData.put("filename", rs.getString("filename"));
            jsonData.put("menu", rs.getString("menu"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:bizlogic.Sensors.java

public static void list(Connection DBcon) throws IOException, ParseException, SQLException {

    Statement st = null;/*from w  w  w . j  a  v a  2s . c  om*/
    ResultSet rs = null;

    try {
        st = DBcon.createStatement();
        rs = st.executeQuery("SELECT * FROM USERCONF.SENSORLIST");

    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(Sensors.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    }
    try {
        FileWriter sensorsFile = new FileWriter("/var/lib/tomcat8/webapps/ROOT/Records/sensors.json");
        sensorsFile.write("");
        sensorsFile.flush();

        JSONParser parser = new JSONParser();

        JSONObject Records = new JSONObject();

        JSONObject operation_Obj = new JSONObject();
        JSONObject operand_Obj = new JSONObject();
        JSONObject unit_Obj = new JSONObject();
        JSONObject name_Obj = new JSONObject();
        JSONObject ip_Obj = new JSONObject();
        JSONObject port_Obj = new JSONObject();

        int _total = 0;

        JSONArray sensorList = new JSONArray();

        while (rs.next()) {

            JSONObject sensor_Obj = new JSONObject();
            int id = rs.getInt("sensor_id");
            String operation = rs.getString("operation");
            int operand = rs.getInt("operand");
            String unit = rs.getString("unit");
            String name = rs.getString("name");
            String ip = rs.getString("IP");
            int port = rs.getInt("port");

            sensor_Obj.put("recid", id);
            sensor_Obj.put("operation", operation);
            sensor_Obj.put("operand", operand);
            sensor_Obj.put("unit", unit);
            sensor_Obj.put("name", name);
            sensor_Obj.put("IP", ip);
            sensor_Obj.put("port", port);

            sensorList.add(sensor_Obj);
            _total++;

        }
        rs.close();

        Records.put("total", _total);
        Records.put("records", sensorList);

        sensorsFile.write(Records.toJSONString());
        sensorsFile.flush();
        sensorsFile.close();
    }

    catch (IOException ex) {
        Logger.getLogger(Sensors.class.getName()).log(Level.WARNING, null, ex);
    }
}

From source file:com.aerothai.database.accessory.AccessoryService.java

public JSONObject GetAccessoryAll(int idunit, String opt) throws Exception {

    Connection dbConn = null;//from  w  ww  . j a v a 2 s. co m
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM accessory";
        if (idunit > 0) {
            query = query + " WHERE idunit =" + idunit;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM accessory WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idaccessory"));
            jsonData.put("details", rs.getString("details"));
            jsonData.put("idunit", rs.getString("idunit"));
            jsonData.put("iddevice_type", rs.getString("iddevice_type"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:com.aerothai.database.radiosignal.RadiosignalService.java

public JSONObject GetRadiosignalAll(String opt) throws Exception {

    Connection dbConn = null;//  w  ww.  j  a  v  a2s. c  o  m
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM radio_signal";

        if (isNotNull(opt))
            query = "SELECT * FROM radio_signal WHERE" + opt;

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idsignal"));
            jsonData.put("idjob", rs.getString("idjob"));
            jsonData.put("asset_no", rs.getString("asset_no"));
            jsonData.put("freq", rs.getString("freq"));
            jsonData.put("rms", rs.getString("rms"));
            jsonData.put("peak", rs.getString("peak"));
            jsonData.put("resid", rs.getString("resid"));
            jsonData.put("time_err", rs.getString("time_err"));
            jsonData.put("power_at_15", rs.getString("power_at_15"));
            jsonData.put("power_at_30", rs.getString("power_at_30"));
            jsonData.put("rec_level", rs.getString("rec_level"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:com.aerothai.database.sparepart.SparepartService.java

public JSONObject GetSparepartAll(int idunit, String opt) throws Exception {

    Connection dbConn = null;// w w  w. ja  v  a  2  s  .c  om
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM spare_part";
        if (idunit > 0) {
            query = query + " WHERE idunit =" + idunit;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM spare_part WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idspare"));
            jsonData.put("details", rs.getString("details"));
            jsonData.put("amount", rs.getString("amount"));
            jsonData.put("price", rs.getString("price"));
            jsonData.put("date", rs.getString("date"));
            jsonData.put("idunit", rs.getString("idunit"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:com.aerothai.database.jobevaluate.JobevaluateService.java

public JSONObject GetJobevaluateAll(int idjob, String opt) throws Exception {

    Connection dbConn = null;//from www  .  java 2  s  .c o m
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM job_evaluate";
        if (idjob > 0) {
            query = query + " WHERE idjob =" + idjob;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM job_evaluate WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idjob_evaluate"));
            jsonData.put("idjob", rs.getString("idjob"));
            jsonData.put("staff", rs.getString("staff"));
            jsonData.put("service", rs.getString("service"));
            jsonData.put("comment", rs.getString("comment"));
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}

From source file:com.aerothai.database.job.JobService.java

public JSONObject GetJobAll(int idunit, String opt) throws Exception {

    Connection dbConn = null;//from  w  w w . ja  v a2 s  . c  om
    JSONObject obj = new JSONObject();
    JSONArray objList = new JSONArray();

    int no = 1;
    //obj.put("draw", 2);
    obj.put("tag", "list");
    obj.put("msg", "error");
    obj.put("status", false);
    try {
        dbConn = DBConnection.createConnection();

        Statement stmt = dbConn.createStatement();
        String query = "SELECT * FROM job";

        if (idunit > 0) {
            query = query + " WHERE idunit =" + idunit;
            if (isNotNull(opt))
                query = query + " AND " + opt;
        } else {
            if (isNotNull(opt))
                query = "SELECT * FROM job WHERE" + opt;
        }

        System.out.println(query);
        ResultSet rs = stmt.executeQuery(query);

        while (rs.next()) {

            JSONObject jsonData = new JSONObject();
            jsonData.put("id", rs.getInt("idjob"));
            jsonData.put("iduser", rs.getString("iduser"));
            jsonData.put("idunit", rs.getString("idunit"));
            jsonData.put("iddevice", rs.getString("iddevice"));
            jsonData.put("service_method", rs.getString("service_method"));
            jsonData.put("service_type", rs.getString("service_type"));
            jsonData.put("details", rs.getString("details"));
            jsonData.put("action", rs.getString("action"));
            jsonData.put("remark", rs.getString("remark"));
            jsonData.put("idstatus", rs.getString("idstatus"));
            jsonData.put("receive_staff", rs.getString("receive_staff"));
            jsonData.put("action_staff", rs.getString("action_staff"));
            jsonData.put("close_staff", rs.getString("close_staff"));
            jsonData.put("wait_date", rs.getString("wait_date"));
            jsonData.put("receive_date", rs.getString("receive_date"));
            jsonData.put("action_date", rs.getString("action_date"));
            jsonData.put("finish_date", rs.getString("finish_date"));
            jsonData.put("eva_date", rs.getString("eva_date"));
            jsonData.put("close_date", rs.getString("close_date"));
            ;
            jsonData.put("no", no);
            objList.add(jsonData);
            no++;

        }
        obj.put("msg", "done");
        obj.put("status", true);
        obj.put("data", objList);
    } catch (SQLException sqle) {
        throw sqle;
    } catch (Exception e) {
        // TODO Auto-generated catch block
        if (dbConn != null) {
            dbConn.close();
        }
        throw e;
    } finally {
        if (dbConn != null) {
            dbConn.close();
        }
    }
    return obj;
}