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 boolean add(E e) 

Source Link

Document

Appends the specified element to the end of this list.

Usage

From source file:org.opencastproject.userdirectory.jpa.JpaUserAndRoleProvider.java

@GET
@Produces(MediaType.APPLICATION_JSON)/*from w w  w . j  av a  2  s . co  m*/
@Path("users.json")
@RestQuery(name = "allusers", description = "Returns a list of users", returnDescription = "Returns a JSON representation of the list of user accounts", restParameters = {
        @RestParameter(defaultValue = "0", description = "The maximum number of items to return per page.", isRequired = false, name = "limit", type = RestParameter.Type.STRING),
        @RestParameter(defaultValue = "0", description = "The page number.", isRequired = false, name = "offset", type = RestParameter.Type.STRING) }, reponses = {
                @RestResponse(responseCode = SC_OK, description = "The user accounts.") })
@SuppressWarnings("unchecked")
public String getUsersAsJson(@QueryParam("limit") int limit, @QueryParam("offset") int offset)
        throws IOException {
    if (limit < 1) {
        limit = 100;
    }
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("users").setMaxResults(limit).setFirstResult(offset);
        q.setParameter("o", securityService.getOrganization().getId());
        List<JpaUser> jpaUsers = q.getResultList();
        JSONArray jsonArray = new JSONArray();
        for (JpaUser user : jpaUsers) {
            Set<String> roles = user.getRoles();
            jsonArray.add(toJson(new User(user.getUsername(), user.getOrganization(),
                    roles.toArray(new String[roles.size()]))));
        }
        return jsonArray.toJSONString();
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:entity.Chart.java

public JSONObject viewReport(HashMap<String, Object> params) throws SQLException {
    JSONObject jo = null;//  w  ww . j ava  2 s  .c  om
    if (params == null) {
        params = new HashMap<String, Object>();
    }
    for (int i = 0; rParams != null && i < rParams.length; i++) {
        if (params == null || !params.containsKey(rParams[i].name)) {
            JSONArray jSONArray = new JSONArray();
            for (int j = 0; j < rParams.length; j++) {
                JSONObject jSONObject = new JSONObject();
                jSONObject.put("Name", rParams[j].name);
                jSONObject.put("DefaultValue", rParams[j].value);
                jSONObject.put("DataType", rParams[j].type);
                jSONObject.put("ReportID", reportID);
                jSONArray.add(jSONObject);
            }
            jo = new JSONObject();
            jo.put("params", jSONArray);
        }

        params.put(rParams[i].name,
                StringHelper.stringToObject(
                        params.get(rParams[i].name) == null ? null : params.get(rParams[i].name).toString(),
                        rParams[i].type));
    }

    return jo == null ? generateReport(params) : jo;
}

From source file:co.edu.unal.arqdsoft.presentacion.servlet.ServletSoporte.java

private Respuesta visitas(JSONObject obj) {
    List<VisitaTecnica> vT = ControlSoporte.visitas();
    JSONArray list = new JSONArray();
    for (VisitaTecnica temp : vT) {
        JSONObject t = new JSONObject();
        t.put("fechavisita", temp.getFechaVisita());
        t.put("cliente", temp.getReporte().getCliente());
        t.put("direccion", temp.getDireccion());
        t.put("id", temp.getId());
        list.add(t);
    }/*from w  w  w . ja va  2s. c  o m*/
    return new Respuesta("", new Contenido(list, ""));
}

From source file:co.edu.unal.arqdsoft.presentacion.servlet.ServletSoporte.java

private Respuesta visitasPorEmpleado(JSONObject obj) {
    List<VisitaTecnica> vT = ControlSoporte.visitasPorEmpleado(Integer.valueOf(obj.get("id").toString()));
    JSONArray list = new JSONArray();
    for (VisitaTecnica temp : vT) {
        JSONObject t = new JSONObject();
        t.put("fechavisita", temp.getFechaVisita());
        t.put("cliente", temp.getReporte().getCliente());
        t.put("direccion", temp.getDireccion());
        t.put("id", temp.getId());
        list.add(t);
    }/*  w  w w.j  a  v a  2 s .c o m*/
    return new Respuesta("", new Contenido(list, ""));
}

From source file:co.edu.unal.arqdsoft.presentacion.servlet.ServletSoporte.java

private Respuesta visitasPorReporte(JSONObject obj) {
    List<VisitaTecnica> vT = ControlSoporte.visitas(Integer.valueOf(obj.get("id").toString()));
    JSONArray list = new JSONArray();
    for (VisitaTecnica temp : vT) {
        JSONObject t = new JSONObject();
        t.put("fechavisita", temp.getFechaVisita());
        t.put("cliente", temp.getReporte().getCliente());
        t.put("direccion", temp.getDireccion());
        t.put("id", temp.getId());
        list.add(t);
    }/*from w ww.jav a2s.  com*/
    return new Respuesta("", new Contenido(list, ""));
}

From source file:com.pjaol.ESB.formatters.JSONFormatter.java

private JSONArray recurseNamedList(NamedList output) {

    int sz = output.size();
    JSONArray jarr = new JSONArray();

    for (int i = 0; i < sz; i++) {
        JSONObject jo = new JSONObject();

        String k = output.getName(i);

        Object v = output.getVal(i);

        if (v instanceof NamedList) {

            jo.put(k, recurseNamedList((NamedList) v));
        } else if (v instanceof Map) {
            jo.put(k, recurseMap((Map) v));
        } else {/*from w  w w. j  av a 2 s.co m*/
            jo.put(k, v);
        }
        jarr.add(jo);

    }

    return jarr;
}

From source file:com.assignment.Product.java

private String getResults(String query, String... params) {
    String result = new String();
    try (Connection conn = getConnection()) {
        PreparedStatement pstmt = conn.prepareStatement(query);
        for (int i = 1; i <= params.length; i++) {
            pstmt.setString(i, params[i - 1]);
        }//from  w w w  .  ja v a 2  s  . c o m
        ResultSet rs = pstmt.executeQuery();
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map productMap = new LinkedHashMap();
            productMap.put("productID", rs.getInt("productID"));
            productMap.put("name", rs.getString("name"));
            productMap.put("description", rs.getString("description"));
            productMap.put("quantity", rs.getInt("quantity"));
            productArr.add(productMap);
        }
        result = productArr.toString();
    } catch (SQLException ex) {
        Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result.replace("},", "},\n");
}

From source file:com.assignment4.products.Pro_details.java

/**
 * Retrieves representation of an instance of com.oracle.products.ProductResource
 * @return an instance of java.lang.String
 *///from  w  w w  .  j  ava 2 s  .  c o  m
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getAllProducts() throws SQLException {
    if (conn == null) {
        return "not connected";
    } else {
        String q = "Select * from products";
        PreparedStatement ps = conn.prepareStatement(q);
        ResultSet rs = ps.executeQuery();
        String r = "";
        JSONArray proArr = new JSONArray();
        while (rs.next()) {
            Map pm = new LinkedHashMap();
            pm.put("productID", rs.getInt("product_id"));
            pm.put("name", rs.getString("name"));
            pm.put("description", rs.getString("description"));
            pm.put("quantity", rs.getInt("quantity"));
            proArr.add(pm);
        }
        r = proArr.toString();
        return r.replace("},", "},\n");
    }

}

From source file:com.assignment4.products.Pro_details.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)// w  ww.j  av  a2 s  .c o  m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (conn == null) {
        return "not connected";
    } else {
        String q = "Select * from products where product_id = ?";
        PreparedStatement ps = conn.prepareStatement(q);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        String result = "";
        JSONArray proArr = new JSONArray();
        while (rs.next()) {
            Map pm = new LinkedHashMap();
            pm.put("productID", rs.getInt("product_id"));
            pm.put("name", rs.getString("name"));
            pm.put("description", rs.getString("description"));
            pm.put("quantity", rs.getInt("quantity"));
            proArr.add(pm);
        }
        result = proArr.toString();

        return result;
    }

}

From source file:ASSINGMENT4.ServletProducts.java

private String getResults(String query, String... params) {
    String result = new String();
    try (Connection conn = getConnection()) {
        PreparedStatement pstmt = conn.prepareStatement(query);
        for (int i = 1; i <= params.length; i++) {
            pstmt.setString(i, params[i - 1]);
        }/*from  w  w w. j av  a  2 s  .c  om*/
        ResultSet rs = pstmt.executeQuery();
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map productMap = new LinkedHashMap();
            productMap.put("product_id", rs.getInt("product_id"));
            productMap.put("product_name", rs.getString("product_name"));
            productMap.put("description", rs.getString("description"));
            productMap.put("quantity", rs.getInt("quantity"));
            productArr.add(productMap);
        }
        result = productArr.toString();
    } catch (SQLException ex) {
        Logger.getLogger(ServletProducts.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result.replace("},", "},\n");
}