Example usage for org.json JSONObject JSONObject

List of usage examples for org.json JSONObject JSONObject

Introduction

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

Prototype

public JSONObject() 

Source Link

Document

Construct an empty JSONObject.

Usage

From source file:com.facebook.login.LoginLogger.java

public void logStartLogin(LoginClient.Request pendingLoginRequest) {
    Bundle bundle = newAuthorizationLoggingBundle(pendingLoginRequest.getAuthId());

    // Log what we already know about the call in start event
    try {/* w ww  .  j a v a2 s .c  o m*/
        JSONObject extras = new JSONObject();
        extras.put(EVENT_EXTRAS_LOGIN_BEHAVIOR, pendingLoginRequest.getLoginBehavior().toString());
        extras.put(EVENT_EXTRAS_REQUEST_CODE, LoginClient.getLoginRequestCode());
        extras.put(EVENT_EXTRAS_PERMISSIONS, TextUtils.join(",", pendingLoginRequest.getPermissions()));
        extras.put(EVENT_EXTRAS_DEFAULT_AUDIENCE, pendingLoginRequest.getDefaultAudience().toString());
        extras.put(EVENT_EXTRAS_IS_REAUTHORIZE, pendingLoginRequest.isRerequest());
        bundle.putString(EVENT_PARAM_EXTRAS, extras.toString());
    } catch (JSONException e) {
    }

    appEventsLogger.logSdkEvent(EVENT_NAME_LOGIN_START, null, bundle);
}

From source file:com.basetechnology.s0.agentserver.field.LocationField.java

public JSONObject toJson() throws JSONException {
    JSONObject json = new JSONObject();
    json.put("type", "location");
    if (symbol.name != null)
        json.put("name", symbol.name);
    if (label != null)
        json.put("label", label);
    if (description != null)
        json.put("description", description);
    if (defaultValue != null)
        json.put("default_value", defaultValue);
    if (minValue != null)
        json.put("min_value", minValue);
    if (maxValue != null)
        json.put("max_value", maxValue);
    if (nominalWidth != 0)
        json.put("nominal_width", nominalWidth);
    if (compute != null)
        json.put("compute", compute);
    return json;// www . j  a va  2  s  . c o m
}

From source file:ai.susi.mind.SusiReader.java

public SusiReader learn(JSONObject json) {

    // initialize temporary json objects
    JSONObject syn = json.has("synonyms") ? json.getJSONObject("synonyms") : new JSONObject();
    JSONArray fill = json.has("filler") ? json.getJSONArray("filler") : new JSONArray();
    JSONObject cat = json.has("categories") ? json.getJSONObject("categories") : new JSONObject();

    // add synonyms
    for (String canonical : syn.keySet()) {
        JSONArray a = syn.getJSONArray(canonical);
        a.forEach(synonym -> synonyms.put(((String) synonym).toLowerCase(), canonical));
    }/*from ww w. ja va2  s  . c o  m*/

    // add filler
    fill.forEach(word -> filler.add((String) word));

    // add categories
    for (String canonical : cat.keySet()) {
        JSONArray a = cat.getJSONArray(canonical);
        a.forEach(synonym -> categories.put(((String) synonym).toLowerCase(), canonical));
    }

    return this;
}

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

public String toJson() {
    JSONObject o = new JSONObject();
    try {//from  ww  w. j a va 2 s . c o  m
        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 {/*from w w  w  .j  av  a 2  s . co  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.ledger.android.u2f.bridge.MainActivity.java

private String createClientData(U2FContext context) {
    try {/*from  w  w  w.jav  a 2  s.co 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   www  . j  ava2s . 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 ww .j a v a2s.  c  o  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:com.fdwills.external.http.JsonHttpResponseHandler.java

@Override
public final void onSuccess(final int statusCode, final Header[] headers, final byte[] responseBytes) {
    if (statusCode != HttpStatus.SC_NO_CONTENT) {
        Runnable parser = new Runnable() {
            @Override// w  ww .ja va  2s .  co m
            public void run() {
                try {
                    final Object jsonResponse = parseResponse(responseBytes);
                    postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            if (jsonResponse instanceof JSONObject) {
                                onSuccess(statusCode, headers, (JSONObject) jsonResponse);
                            } else if (jsonResponse instanceof JSONArray) {
                                onSuccess(statusCode, headers, (JSONArray) jsonResponse);
                            } else if (jsonResponse instanceof String) {
                                onFailure(statusCode, headers, (String) jsonResponse,
                                        new JSONException("Response cannot be parsed as JSON data"));
                            } else {
                                onFailure(statusCode, headers, new JSONException(
                                        "Unexpected response type " + jsonResponse.getClass().getName()),
                                        (JSONObject) null);
                            }

                        }
                    });
                } catch (final JSONException ex) {
                    postRunnable(new Runnable() {
                        @Override
                        public void run() {
                            onFailure(statusCode, headers, ex, (JSONObject) null);
                        }
                    });
                }
            }
        };
        if (!getUseSynchronousMode()) {
            new Thread(parser).start();
        } else {
            // In synchronous mode everything should be run on one thread
            parser.run();
        }
    } else {
        onSuccess(statusCode, headers, new JSONObject());
    }
}

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   w  w  w.ja v  a 2s  .c o m

    // 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);
}