Example usage for org.json JSONArray put

List of usage examples for org.json JSONArray put

Introduction

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

Prototype

public JSONArray put(Object value) 

Source Link

Document

Append an object value.

Usage

From source file:com.android.browser.GearsSettingsDialog.java

/**
 * Computes the difference between the original permissions and the
 * current ones. Returns a json-formatted string.
 * It is used by the Settings dialog./*  www  .j  a v a 2s .  c  o  m*/
 */
public String computeDiff(boolean modif) {
    String ret = null;
    try {
        JSONObject results = new JSONObject();
        JSONArray permissions = new JSONArray();

        for (int i = 0; modif && i < mOriginalPermissions.size(); i++) {
            OriginPermissions original = mOriginalPermissions.get(i);
            OriginPermissions current = mCurrentPermissions.get(i);
            JSONObject permission = new JSONObject();
            boolean modifications = false;

            for (int j = 0; j < mPermissions.size(); j++) {
                PermissionType type = mPermissions.get(j);

                if (current.getPermission(type) != original.getPermission(type)) {
                    JSONObject state = new JSONObject();
                    state.put("permissionState", current.getPermission(type));
                    permission.put(type.getName(), state);
                    modifications = true;
                }
            }

            if (modifications) {
                permission.put("name", current.getOrigin());
                permissions.put(permission);
            }
        }
        results.put("modifiedOrigins", permissions);
        ret = results.toString();
    } catch (JSONException e) {
        Log.e(TAG, "JSON exception ", e);
    }
    return ret;
}

From source file:com.marpies.ane.facedetection.functions.DetectFacesFunction.java

private JSONArray getFacesJSONArray(SparseArray<Face> faces) {
    int numFaces = faces.size();
    JSONArray facesResult = new JSONArray();
    for (int i = 0; i < numFaces; i++) {
        Face face = faces.valueAt(i);//from   w  w  w .  j ava 2  s. co  m
        String faceJSON = getFaceJSON(face);
        if (faceJSON != null) {
            facesResult.put(faceJSON);
        } else {
            AIR.log("Error making JSON out of Face object");
        }
    }
    AIR.log("Parsed " + facesResult.length() + " faces");
    return facesResult;
}

From source file:com.bluepandora.therap.donatelife.jsonbuilder.HospitalJson.java

public static JSONObject getHospitalJson(ResultSet result) throws JSONException {
    JSONObject jsonObject;//from  w w w  .  j a va  2s  .  c o  m
    JSONArray jsonArray = new JSONArray();
    try {

        while (result.next()) {
            jsonObject = new JSONObject();
            jsonObject.put(DIST_ID, result.getString("dist_id"));
            jsonObject.put(HOSP_ID, result.getString("hospital_id"));
            jsonObject.put(HOSP_NAME, result.getString("hospital_name"));
            jsonObject.put(HOSP_BNAME, result.getString("hospital_bname"));
            jsonArray.put(jsonObject);
        }
        jsonObject = new JSONObject();
        jsonObject.put(HOSPITAL, jsonArray);
        jsonObject.put(DONE, 1);

    } catch (SQLException error) {

        Debug.debugLog("HOSPITAL JSON ERROR: ", error);
        jsonObject = new JSONObject();
        jsonObject.put(DONE, 0);
    }
    return jsonObject;
}

From source file:fr.bmartel.android.fadecandy.model.FadecandyColor.java

public JSONObject toJsonObject() {

    JSONObject color = new JSONObject();

    try {// w  ww .  j ava  2  s .com

        JSONArray whitepoints = new JSONArray();
        for (Float item : mWhitepoints) {
            whitepoints.put(item);
        }

        color.put(Constants.CONFIG_GAMMA, mGamma);
        color.put(Constants.CONFIG_WHITEPOINT, whitepoints);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return color;
}

From source file:com.mobage.air.extension.social.jp.Textdata_getEntries.java

@Override
public FREObject call(final FREContext context, FREObject[] args) {
    try {//from  w  w  w. j  a v a 2  s. co m
        ArgsParser a = new ArgsParser(args);
        final String groupName = a.nextString();
        final ArrayList<String> entryIds = a.nextStringArrayList();
        final String onSuccessId = a.nextString();
        final String onErrorId = a.nextString();
        a.finish();

        com.mobage.android.social.jp.Textdata.OnGetEntriesComplete cb = new com.mobage.android.social.jp.Textdata.OnGetEntriesComplete() {
            @Override
            public void onSuccess(ArrayList<com.mobage.android.social.jp.Textdata.TextdataEntry> entries) {
                try {
                    JSONArray args = new JSONArray();
                    JSONArray jsonEntries = new JSONArray();
                    for (com.mobage.android.social.jp.Textdata.TextdataEntry entry : entries) {
                        jsonEntries.put(Convert.jpTextdataEntryToJSON(entry));
                    }
                    args.put(jsonEntries);
                    Dispatcher.dispatch(context, onSuccessId, args);
                } catch (Exception e) {
                    Dispatcher.exception(context, e);
                }
            }

            @Override
            public void onError(Error error) {
                Dispatcher.dispatch(context, onErrorId, error);
            }
        };

        com.mobage.android.social.jp.Textdata.getEntries(groupName, entryIds, cb);
    } catch (Exception e) {
        Dispatcher.exception(context, e);
    }
    return null;
}

From source file:Logica.SesionWeb.java

@Override
public void notificarUsuariosLogeados(HashMap<String, Usuario> usuariosConectados) {
    try {//from   w ww . java 2s  . com
        if (sesion.isOpen()) {
            JSONObject objetoJSON = new JSONObject();
            JSONArray usuarios = new JSONArray();
            for (Map.Entry<String, Usuario> entry : usuariosConectados.entrySet()) {
                String nick = entry.getKey();
                usuarios.put(nick);
            }
            objetoJSON.put("usuarios", usuarios);
            objetoJSON.append("tipo", "CONTACTOS");
            sesion.getBasicRemote().sendText(objetoJSON.toString());
        }
    } catch (IOException ex) {
        Logger.getLogger(SesionWeb.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:com.comcast.oscar.sql.queries.DocsisSqlQuery.java

/**
 * //  w  w  w.  j  ava  2s . co  m
 * @param iRowID
 * @param iParentID
 * @param aliTlvEncodeHistory   
 * @return JSONObject
 * @throws JSONException */
private JSONObject recursiveTlvDefinitionBuilder(Integer iRowID, Integer iParentID,
        ArrayList<Integer> aliTlvEncodeHistory) throws JSONException {

    Statement parentCheckStatement = null, getRowDefinitionStatement = null;

    ResultSet resultSetParentCheck = null, resultSetGetRowDefinition = null;

    aliTlvEncodeHistory.add(getTypeFromRowID(iParentID));

    String sqlQuery;

    JSONObject tlvJsonObj = null;

    // This query will check for child rows that belong to a parent row
    sqlQuery = "SELECT " + "   ID ," + "   TYPE ," + "   TLV_NAME," + "   PARENT_ID " + "FROM "
            + "   DOCSIS_TLV_DEFINITION " + "WHERE " + "   PARENT_ID = '" + iRowID + "'";

    try {

        //Create statement
        parentCheckStatement = sqlConnection.createStatement();

        //Get Result Set of Query
        resultSetParentCheck = parentCheckStatement.executeQuery(sqlQuery);

    } catch (SQLException e) {
        e.printStackTrace();
    }

    /* ******************************************************************************************************
     * If resultSet return an empty, this means that the row does not have a child and is not a Major TLV
     * *****************************************************************************************************/

    try {

        if ((SqlConnection.getRowCount(resultSetParentCheck) == 0) && (iParentID != MAJOR_TLV)) {

            // This query will check for child rows that belong to a parent row
            sqlQuery = "SELECT * FROM " + "   DOCSIS_TLV_DEFINITION " + "   WHERE ID = '" + iRowID + "'";

            //Create statement to get Rows from ROW ID's
            getRowDefinitionStatement = sqlConnection.createStatement();

            //Get Result Set
            resultSetGetRowDefinition = getRowDefinitionStatement.executeQuery(sqlQuery);

            //Advance to next index in result set, this is needed
            resultSetGetRowDefinition.next();

            /*************************************************
             *             Assemble JSON OBJECT
             *************************************************/

            tlvJsonObj = new JSONObject();

            tlvJsonObj.put(Dictionary.TYPE, resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_TYPE));
            tlvJsonObj.put(Dictionary.TLV_NAME,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_TLV_NAME));
            tlvJsonObj.put(Dictionary.LENGTH_MIN,
                    resultSetGetRowDefinition.getInt(Dictionary.DB_TBL_COL_LENGTH_MIN));
            tlvJsonObj.put(Dictionary.LENGTH_MAX,
                    resultSetGetRowDefinition.getInt(Dictionary.DB_TBL_COL_LENGTH_MAX));
            tlvJsonObj.put(Dictionary.SUPPORTED_VERSIONS,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_SUPPORTED_VERSIONS));
            tlvJsonObj.put(Dictionary.DATA_TYPE,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_DATA_TYPE));
            tlvJsonObj.put(Dictionary.ARE_SUBTYPES, false);
            tlvJsonObj.put(Dictionary.BYTE_LENGTH,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_BYTE_LENGTH));

            aliTlvEncodeHistory.add(resultSetGetRowDefinition.getInt(Dictionary.DB_TBL_COL_TYPE));
            tlvJsonObj.put(Dictionary.PARENT_TYPE_LIST, aliTlvEncodeHistory);

            if (debug)
                System.out.println(tlvJsonObj.toString());

        } else if (iParentID == MAJOR_TLV) {

            // This query will check for child rows that belong to a parent row
            sqlQuery = "SELECT * FROM " + "   DOCSIS_TLV_DEFINITION " + "   WHERE ID = '" + iRowID + "'";

            getRowDefinitionStatement = sqlConnection.createStatement();

            resultSetGetRowDefinition = getRowDefinitionStatement.executeQuery(sqlQuery);

            resultSetGetRowDefinition.next();

            /*************************************************
             *             Assemble JSON OBJECT
             *************************************************/

            tlvJsonObj = new JSONObject();

            tlvJsonObj.put(Dictionary.TYPE, resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_TYPE));
            tlvJsonObj.put(Dictionary.TLV_NAME,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_TLV_NAME));
            tlvJsonObj.put(Dictionary.LENGTH_MIN,
                    resultSetGetRowDefinition.getInt(Dictionary.DB_TBL_COL_LENGTH_MIN));
            tlvJsonObj.put(Dictionary.LENGTH_MAX,
                    resultSetGetRowDefinition.getInt(Dictionary.DB_TBL_COL_LENGTH_MAX));
            tlvJsonObj.put(Dictionary.SUPPORTED_VERSIONS,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_SUPPORTED_VERSIONS));
            tlvJsonObj.put(Dictionary.DATA_TYPE,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_DATA_TYPE));
            tlvJsonObj.put(Dictionary.ARE_SUBTYPES, false);
            tlvJsonObj.put(Dictionary.BYTE_LENGTH,
                    resultSetGetRowDefinition.getString(Dictionary.DB_TBL_COL_BYTE_LENGTH));

            aliTlvEncodeHistory.add(resultSetGetRowDefinition.getInt("TYPE"));
            tlvJsonObj.put(Dictionary.PARENT_TYPE_LIST, aliTlvEncodeHistory);

            if (debug)
                System.out.println(tlvJsonObj.toString());
        }

        if (SqlConnection.getRowCount(resultSetParentCheck) > 0) {

            // This query will check for child rows that belong to a parent row
            sqlQuery = "SELECT " + "   TYPE , " + "   TLV_NAME," + "   PARENT_ID" + " FROM "
                    + "   DOCSIS_TLV_DEFINITION " + "   WHERE ID = '" + iRowID + "'";

            try {

                Integer iParentIdTemp = null;

                JSONArray tlvJsonArray = new JSONArray();

                while (resultSetParentCheck.next()) {

                    iParentIdTemp = resultSetParentCheck.getInt(Dictionary.DB_TBL_COL_PARENT_ID);

                    ArrayList<Integer> aliTlvEncodeHistoryNext = new ArrayList<Integer>();

                    aliTlvEncodeHistoryNext.addAll(aliTlvEncodeHistory);

                    if (debug)
                        System.out.println("aliTlvEncodeHistoryNext: " + aliTlvEncodeHistoryNext);

                    aliTlvEncodeHistoryNext.remove(aliTlvEncodeHistoryNext.size() - 1);

                    //Keep processing each row using recursion until you get to to the bottom of the tree
                    tlvJsonArray.put(
                            recursiveTlvDefinitionBuilder(resultSetParentCheck.getInt(Dictionary.DB_TBL_COL_ID),
                                    iParentIdTemp, aliTlvEncodeHistoryNext));
                }

                //Get Parent Definition
                tlvJsonObj = getRowDefinitionViaRowId(iParentIdTemp);

                tlvJsonObj.put(Dictionary.SUBTYPE_ARRAY, tlvJsonArray);

            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return tlvJsonObj;
}

From source file:uk.ac.imperial.presage2.web.SimulationsTreeServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    logRequest(req);/*from w  w w.  j a v a  2 s .c  o  m*/
    // check which node to check from
    String node = req.getParameter("node");
    if (node == null) {
        resp.setStatus(400);
        return;
    }
    long nodeId;
    if (node.equalsIgnoreCase("root")) {
        nodeId = 0;
    } else {
        try {
            nodeId = Long.parseLong(node);
        } catch (NumberFormatException e) {
            resp.setStatus(400);
            return;
        }
    }

    List<PersistentSimulation> sims = new LinkedList<PersistentSimulation>();
    for (Long simId : this.sto.getSimulations()) {
        PersistentSimulation sim = this.sto.getSimulationById(simId);
        PersistentSimulation parent = sim.getParentSimulation();
        if ((nodeId == 0 && parent == null) || (parent != null && nodeId > 0 && parent.getID() == nodeId)) {
            sims.add(sim);
        }
    }
    // build JSON response
    try {
        JSONObject jsonResp = new JSONObject();
        JSONArray jsonSims = new JSONArray();
        for (PersistentSimulation sim : sims) {
            jsonSims.put(SimulationServlet.simulationToJSON(sim));
        }
        jsonResp.put("data", jsonSims);
        jsonResp.put("success", true);
        resp.setStatus(200);
        resp.getWriter().write(jsonResp.toString());
    } catch (JSONException e) {
        resp.setStatus(500);
    }
}

From source file:com.basho.riak.client.http.mapreduce.filter.ToUpperFilter.java

public JSONArray toJson() {
    JSONArray filter = new JSONArray();
    filter.put("to_upper");
    return filter;
}

From source file:org.exoplatform.social.portlet.UISpacesToolBarPortlet.java

private JSONArray getChildrenAsJSON(UserNode userNode) throws Exception {
    if (userNode == null) {
        return null;
    }/*from   w ww.  j  a va2  s  .  c  om*/

    NodeChangeQueue<UserNode> queue = new NodeChangeQueue<UserNode>();
    //Scope.CHILDREN ???
    SpaceUtils.getUserPortal().updateNode(userNode, toolbarScope, queue);
    for (NodeChange<UserNode> change : queue) {
        if (change instanceof NodeChange.Removed) {
            UserNode deletedNode = ((NodeChange.Removed<UserNode>) change).getTarget();
            if (hasRelationship(deletedNode, userNode)) {
                return null;
            }
        }
    }
    Collection<UserNode> childs = userNode.getChildren();

    JSONArray jsChilds = new JSONArray();
    WebuiRequestContext context = WebuiRequestContext.getCurrentInstance();
    MimeResponse res = context.getResponse();
    for (UserNode child : childs) {
        jsChilds.put(toJSON(child, userNode.getNavigation().getKey().getName(), res));
    }
    return jsChilds;
}