Example usage for org.json JSONObject put

List of usage examples for org.json JSONObject put

Introduction

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

Prototype

public JSONObject put(String key, Object value) throws JSONException 

Source Link

Document

Put a key/value pair in the JSONObject.

Usage

From source file:org.onepf.oms.data.SkuDetails.java

public String toJson() {
    JSONObject o = new JSONObject();
    try {/*w w  w.  j a v a  2 s.  c  om*/
        o.put("productId", _sku);
        o.put("type", _type);
        o.put("price", _price);
        o.put("title", _title);
        o.put("description", _description);
    } catch (JSONException e) {
        Log.e(BillingApplication.TAG, "Couldn't serialize " + getClass().getSimpleName());
        return "";
    }
    return o.toString();
}

From source file:com.grarak.kerneladiutor.utils.database.ProfileDB.java

public void putProfile(String name, LinkedHashMap<String, String> commands) {
    try {//  w w w . java  2  s  .  c o m

        JSONObject items = new JSONObject();
        items.put("name", name);

        JSONArray commandArray = new JSONArray();
        for (int i = 0; i < commands.size(); i++) {
            JSONObject item = new JSONObject();
            item.put("path", commands.keySet().toArray()[i]);
            item.put("command", commands.values().toArray()[i]);
            commandArray.put(item);
        }

        items.put("commands", commandArray);

        items.put("id", UUID.randomUUID());

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

From source file:com.grarak.kerneladiutor.utils.database.ProfileDB.java

public int updateDB(Context context) {
    ProfileDB profileDB = new ProfileDB(context);
    int result = 0;

    List<ProfileDB.ProfileItem> profileItems = profileDB.getAllProfiles();
    for (int i = 0; i < profileItems.size(); i++) {
        JSONObject temp = profileItems.get(i).getItem();
        if (profileItems.get(i).getID().equals("111")) {
            try {
                temp.put("id", UUID.randomUUID());
                putItem(temp);//from   w  w w.ja  va  2  s.  co  m
                profileDB.commit();
                result = 1;
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    return result;
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createClientData(U2FContext context) {
    try {//from   www  .jav  a  2 s  . c o m
        JSONObject clientData = new JSONObject();
        clientData.put(TAG_JSON_TYP, (context.isSign() ? SIGN_RESPONSE_TYP : REGISTER_RESPONSE_TYP));
        clientData.put(TAG_JSON_CHALLENGE, Base64.encodeToString(context.getChallenge(),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        clientData.put(TAG_JSON_ORIGIN, context.getAppId());
        clientData.put(TAG_JSON_CID_PUBKEY, CID_UNAVAILABLE);
        return clientData.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding client data");
        return null;
    }
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createU2FResponseSign(U2FContext context, byte[] signature) {
    try {//from ww w  .  ja  va 2 s.c o  m
        JSONObject response = new JSONObject();
        response.put(TAG_JSON_TYPE, SIGN_RESPONSE_TYPE);
        response.put(TAG_JSON_REQUESTID, context.getRequestId());
        JSONObject responseData = new JSONObject();
        responseData.put(TAG_JSON_KEYHANDLE, Base64.encodeToString(context.getChosenKeyHandle(),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        responseData.put(TAG_JSON_SIGNATUREDATA, Base64.encodeToString(signature, 0, signature.length - 2,
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        String clientData = createClientData(context);
        responseData.put(TAG_JSON_CLIENTDATA, Base64.encodeToString(clientData.getBytes("UTF-8"),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        response.put(TAG_JSON_RESPONSEDATA, responseData);
        return response.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding request");
        return null;
    }
}

From source file:com.ledger.android.u2f.bridge.MainActivity.java

private String createU2FResponseRegister(U2FContext context, byte[] registerResponse) {
    try {// w  w w . j a va 2s  . co  m
        JSONObject response = new JSONObject();
        response.put(TAG_JSON_TYPE, REGISTER_RESPONSE_TYPE);
        response.put(TAG_JSON_REQUESTID, context.getRequestId());
        JSONObject responseData = new JSONObject();
        responseData.put(TAG_JSON_REGISTRATIONDATA, Base64.encodeToString(registerResponse, 0,
                registerResponse.length - 2, Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        responseData.put(TAG_JSON_VERSION, VERSION_U2F_V2);
        String clientData = createClientData(context);
        responseData.put(TAG_JSON_CLIENTDATA, Base64.encodeToString(clientData.getBytes("UTF-8"),
                Base64.URL_SAFE | Base64.NO_WRAP | Base64.NO_PADDING));
        response.put(TAG_JSON_RESPONSEDATA, responseData);
        return response.toString();
    } catch (Exception e) {
        Log.e(TAG, "Error encoding request");
        return null;
    }
}

From source file:de.decoit.visa.http.ajax.handlers.RemoveVLANHandler.java

@Override
public void handle(HttpExchange he) throws IOException {
    log.info(he.getRequestURI().toString());

    // Get the URI of the request and extract the query string from it
    QueryString queryParameters = new QueryString(he.getRequestURI());

    // Create StringBuilder for the response
    String response = null;//from  ww w.  j  av  a 2s  .c om

    // Check if the query parameters are valid for this handler
    if (this.checkQueryParameters(queryParameters)) {
        // Any exception thrown during object creation will cause
        // failure of the AJAX request
        try {
            JSONObject rv = new JSONObject();

            TEBackend.TOPOLOGY_STORAGE.removeVLAN(queryParameters.get("locname").get());

            rv.put("status", AJAXServer.AJAX_SUCCESS);
            rv.put("topology", TEBackend.TOPOLOGY_STORAGE.genTopologyJSON());

            // Set the HTTP response to the identifier string of the new
            // component
            response = rv.toString();
        } catch (Throwable ex) {
            TEBackend.logException(ex, log);

            try {
                // Synchronize the topology with the RDF model to resolve
                // any errors caused by the caught exception
                TEBackend.RDF_MANAGER.syncTopologyToRDF();

                JSONObject rv = new JSONObject();
                rv.put("status", AJAXServer.AJAX_ERROR_EXCEPTION);
                rv.put("type", ex.getClass().getSimpleName());
                rv.put("message", ex.getMessage());
                rv.put("topology", TEBackend.TOPOLOGY_STORAGE.genTopologyJSON());
                response = rv.toString();
            } catch (Throwable e) {
                // Exception during synchronization, the model may have been
                // corrupted so the whole backend was cleared
                JSONObject rv = new JSONObject();
                try {
                    rv.put("status", AJAXServer.AJAX_ERROR_EXCEPTION_UNRESOLVED);
                    rv.put("topology", TEBackend.TOPOLOGY_STORAGE.genTopologyJSON());
                } catch (JSONException exc) {
                    /* Ignore */
                }

                response = rv.toString();
            }
        }

    } else {
        // Missing or malformed query string, set response to error code
        JSONObject rv = new JSONObject();
        try {
            rv.put("status", AJAXServer.AJAX_ERROR_MISSING_ARGS);
        } catch (JSONException exc) {
            /* Ignore */
        }

        response = rv.toString();
    }

    // Send the response
    sendResponse(he, response);
}

From source file:com.browseengine.bobo.server.protocol.BrowseJSONSerializer.java

/**
 * TODO: need to add support for multiple values.
 * @param doc// w  w w . j a  v a 2  s.c om
 * @return
 * @throws JSONException 
 */
public static JSONObject serializeValues(Map<String, String[]> values) throws JSONException {
    JSONObject obj = new JSONObject();
    Iterator<String> iter = values.keySet().iterator();
    while (iter.hasNext()) {
        String name = iter.next();
        String[] vals = values.get(name);
        if (vals.length > 0) {
            obj.put(name, vals[0]);
        }
    }
    return obj;
}

From source file:com.browseengine.bobo.server.protocol.BrowseJSONSerializer.java

public static JSONObject serializeHits(BrowseHit hit) throws JSONException {
    JSONObject obj = new JSONObject();
    obj.put("doc", serializeValues(hit.getFieldValues()));
    obj.put("docid", hit.getDocid());
    obj.put("score", hit.getScore());
    return obj;/* w w  w . j  av  a 2  s . c om*/
}

From source file:com.browseengine.bobo.server.protocol.BrowseJSONSerializer.java

public static String serialize(BrowseResult result) throws JSONException {
    JSONObject obj = new JSONObject();
    if (result != null) {
        obj.put("time", ((double) result.getTime()) / 1000.0);
        obj.put("hitCount", result.getNumHits());
        obj.put("totalDocs", result.getTotalDocs());

        // serialize choices
        JSONObject choices = new JSONObject();
        Set<Entry<String, FacetAccessible>> facetAccessors = result.getFacetMap().entrySet();
        for (Entry<String, FacetAccessible> entry : facetAccessors) {
            JSONObject choiceObject = new JSONObject();
            JSONArray choiceValArray = new JSONArray();

            choiceObject.put("choicelist", choiceValArray);
            int k = 0;

            String name = entry.getKey();
            FacetAccessible facets = entry.getValue();

            List<BrowseFacet> facetList = facets.getFacets();
            for (BrowseFacet facet : facetList) {
                JSONObject choice = new JSONObject();
                choice.put("val", facet.getValue());
                choice.put("hits", facet.getHitCount());
                choiceValArray.put(k++, choice);
            }/*from  w  w w  .j a v a 2 s  . co m*/
            choices.put(name, choiceObject);
        }
        obj.put("choices", choices);

        JSONArray hitsArray = new JSONArray();
        BrowseHit[] hits = result.getHits();
        if (hits != null && hits.length > 0) {
            for (int i = 0; i < hits.length; ++i) {
                hitsArray.put(i, serializeHits(hits[i]));
            }
        }
        obj.put("hits", hitsArray);
        // serialize documents
    }
    return obj.toString();
}