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:com.ocs.indaba.controlpanel.controller.notif.NotificationsController.java

private JSONObject importResultToJson(NotificationItemImporter importer) {
    JSONObject root = new JSONObject();
    root.put("errCount", importer.getErrorCount());
    if (importer.getErrorCount() > 0) {
        JSONArray errorArray = new JSONArray();
        List<String> errors = importer.getErrors();
        for (String err : errors) {
            errorArray.add(err);
        }/*  www  . j ava 2s.  c  o m*/
        root.put("errors", errorArray);
    }

    List<ImpNotificationItem> notifs = importer.getNotifs();
    root.put("count", (notifs == null) ? 0 : notifs.size());
    return root;
}

From source file:com.Assignment4.Prod.java

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

}

From source file:com.Assignment4.Prod.java

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_JSON)//from  www  .j  a va  2 s .co  m
public String getproduct(@PathParam("id") int id) throws SQLException {

    if (connect == null) {
        return "not connected";
    } else {
        String query = "Select * from product where product_id = ?";
        PreparedStatement prepstmt = connect.prepareStatement(query);
        prepstmt.setInt(1, id);
        ResultSet rs = prepstmt.executeQuery();
        String result = "";
        JSONArray productArr = new JSONArray();
        while (rs.next()) {
            Map pMap = new LinkedHashMap();
            pMap.put("productID", rs.getInt("product_id"));
            pMap.put("name", rs.getString("name"));
            pMap.put("description", rs.getString("description"));
            pMap.put("quantity", rs.getInt("quantity"));
            productArr.add(pMap);
        }
        result = productArr.toString();

        return result;
    }

}

From source file:edu.vt.vbi.patric.common.ExpressionDataCollection.java

public JSONArray append(JSONArray array, String input) {
    JSONArray items = null;/*  w w w.j a v  a2  s  .com*/

    if (input.equals(CONTENT_SAMPLE)) {
        items = this.sample;
    } else if (input.equals(CONTENT_EXPRESSION)) {
        items = this.expression;
    }

    assert items != null;
    for (Object item : items) {
        JSONObject obj = (JSONObject) item;
        array.add(obj);
    }
    return array;
}

From source file:com.googlecode.fascinator.portal.process.RecordProcessor.java

/**
 * 'post' - processing method./*  www.ja  v  a 2s.c  o m*/
 * 
 * he 'inputKey' entry in the dataMap contains ids queued for resending.
 * This is merged with the 'includeList' and persisted on the config file.
 * The 'lastRun' is updated and persisted as well.
 * 
 * @param id
 * @param inputKey
 * @param configFilePath
 * @param dataMap
 * @return
 * @throws Exception
 */
private boolean postProcess(String id, String inputKey, String configFilePath, HashMap<String, Object> dataMap)
        throws Exception {
    File configFile = new File(configFilePath);
    SimpleDateFormat dtFormat = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss'Z'");
    JsonSimple config = new JsonSimple(configFile);
    config.getJsonObject().put("lastrun", dtFormat.format(new Date()));
    Collection<String> oids = (Collection<String>) dataMap.get(inputKey);
    JSONArray includedArr = config.getArray("includeList");
    if (oids != null && oids.size() > 0) {
        // some oids failed, writing it to inclusion list so it can be sent
        // next time...
        if (includedArr == null) {
            includedArr = config.writeArray("includeList");
        }
        includedArr.clear();
        for (String oid : oids) {
            includedArr.add(oid);
        }
    } else {
        // no oids failed, all good, clearing the list...
        if (includedArr != null && includedArr.size() > 0) {
            includedArr.clear();
        }
    }
    FileWriter writer = new FileWriter(configFile);
    writer.write(config.toString(true));
    writer.close();
    return true;
}

From source file:com.hortonworks.pig.udf.ToJson.java

/**
 * Convert a Pig Bag to a JSON array/*from  w ww  .  j av a2 s .c  o m*/
 */
@SuppressWarnings("unchecked")
private JSONArray bagToJson(DataBag bag, FieldSchema bagFieldSchema) throws ExecException {
    JSONArray array = new JSONArray();
    FieldSchema tupleSchema = null;
    try {
        tupleSchema = bagFieldSchema.schema.getField(0);
    } catch (Exception e) {
        LOG.error("Unable to parse schema: " + bagFieldSchema);
        e.printStackTrace();
    }

    for (Tuple t : bag) {
        JSONObject recJson = tupleToJson(t, tupleSchema);
        array.add(recJson);
    }
    return array;
}

From source file:com.products.products.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   ww  w  .j  a  v  a 2s .com*/
        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(products.class.getName()).log(Level.SEVERE, null, ex);
    }
    return result.replace("},", "},\n");
}

From source file:com.web.mavenproject6.controller.CameraController.java

@RequestMapping(value = "/plist", method = RequestMethod.GET)
public @ResponseBody String getPersonalName(@RequestParam("personalName") String personalName) {
    JSONArray ar = new JSONArray();

    List<personal> pList = personalService.getAll();
    for (personal p : pList) {
        String a = p.getAccessNumber() + " " + p.getFname() + " " + p.getSname() + " " + p.getTname();
        if (a.contains(personalName)) {
            ar.add(p.toString());
        }//w  w w.ja v  a  2  s. co m
    }
    return ar.toString();

}

From source file:mml.handler.scratch.ScratchVersion.java

/**
 * Convert this version to JSON so it can stored in the database
 * @return the entire version as a string
 *//*from  w  w  w  . j  a v a  2 s. c o m*/
public String toJSON() {
    JSONObject jObj = new JSONObject();
    if (layers != null) {
        Set<String> keys = layers.keySet();
        String[] arr = new String[keys.size()];
        keys.toArray(arr);
        Arrays.sort(arr);
        jObj.put(JSONKeys.VERSION1, version);
        if (this.longName != null)
            jObj.put(JSONKeys.LONGNAME, longName);
        Calendar cal = Calendar.getInstance();
        // timestap conversion to JSON
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jObj.put(JSONKeys.TIME, sdf.format(cal.getTime()));
        jObj.put("dbase", dbase);
        jObj.put("dirty", dirty);
        JSONArray jArr = new JSONArray();
        jObj.put("layers", jArr);
        for (int i = 0; i < arr.length; i++) {
            JSONObject jLayer = new JSONObject();
            jLayer.put(JSONKeys.NAME, arr[i]);
            jLayer.put(JSONKeys.BODY, layers.get(arr[i]));
            jArr.add(jLayer);
        }
    }
    return jObj.toJSONString();
}

From source file:com.Assignment4.Assign.java

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

}