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.liferay.mobile.android.v7.journalfolder.JournalFolderService.java

public Integer getFoldersAndArticlesCount(long groupId, long userId, long folderId, int status)
        throws Exception {
    JSONObject _command = new JSONObject();

    try {//from  w  w w.  j a va2s  . co m
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("userId", userId);
        _params.put("folderId", folderId);
        _params.put("status", status);

        _command.put("/journal.journalfolder/get-folders-and-articles-count", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getInt(0);
}

From source file:com.liferay.mobile.android.v7.journalfolder.JournalFolderService.java

public Integer getFoldersAndArticlesCount(long groupId, long folderId, int status) throws Exception {
    JSONObject _command = new JSONObject();

    try {// ww w.j a va  2  s  . c  om
        JSONObject _params = new JSONObject();

        _params.put("groupId", groupId);
        _params.put("folderId", folderId);
        _params.put("status", status);

        _command.put("/journal.journalfolder/get-folders-and-articles-count", _params);
    } catch (JSONException _je) {
        throw new Exception(_je);
    }

    JSONArray _result = session.invoke(_command);

    if (_result == null) {
        return null;
    }

    return _result.getInt(0);
}

From source file:at.alladin.rmbt.client.SpeedItem.java

public JSONObject toJSON() throws JSONException {
    final JSONObject result = new JSONObject();
    result.put("direction", upload ? "upload" : "download");
    result.put("thread", thread);
    result.put("time", time);
    result.put("bytes", bytes);
    return result;
}

From source file:com.controlj.addon.weather.util.ResponseWriter.java

public ResponseWriter(HttpServletResponse resp) {
    this.resp = resp;
    this.jsonRoot = new JSONObject();
}

From source file:com.controlj.addon.weather.util.ResponseWriter.java

public void addValidationError(String fieldName, String message) {
    try {/*from w  w  w.  ja  v  a2 s .c o  m*/
        JSONArray errors = getOrCreateArray(ERROR_LIST);
        JSONObject error = new JSONObject();
        error.put("errortype", "validation");
        error.put("field", fieldName);
        error.put("message", message);
        errors.put(error);
    } catch (JSONException e) {
        Logging.println("Error trying to write out errors with JSON", e);
    }
}

From source file:com.controlj.addon.weather.util.ResponseWriter.java

public void addError(String message) {
    try {/*from w ww  . ja  v a  2s  .  c  o m*/
        JSONArray errors = getOrCreateArray(ERROR_LIST);
        JSONObject error = new JSONObject();
        error.put("errortype", "general");
        error.put("message", message);
        errors.put(error);
    } catch (JSONException e) {
        Logging.println("Error trying to write out errors with JSON", e);
    }
}

From source file:com.controlj.addon.weather.util.ResponseWriter.java

private JSONObject getOrCreateObject(String childName) throws JSONException {
    JSONObject result;//from w w  w.  j  a  v  a 2  s. c o  m
    if (jsonRoot.has(childName)) {
        result = (JSONObject) jsonRoot.get(childName);
    } else {
        result = new JSONObject();
        jsonRoot.put(childName, result);
    }
    return result;
}

From source file:org.cloudfoundry.client.lib.util.JsonUtil.java

public static JSONObject convertMapToJson(Map<String, Object> map) throws JSONException {
    JSONObject res = new JSONObject();
    for (String key : map.keySet()) {
        res.put(key, map.get(key));/*  w  ww.j  a  v a  2 s  . co  m*/
    }
    return res;
}

From source file:com.layer.atlas.messenger.AtlasIdentityProvider.java

private String[] refreshContacts(boolean requestIdentityToken, String nonce, String userName) {
    try {/*from w w  w. jav a2s.  c o m*/
        String url = "https://layer-identity-provider.herokuapp.com/apps/" + appId + "/atlas_identities";
        HttpPost post = new HttpPost(url);
        post.setHeader("Content-Type", "application/json");
        post.setHeader("Accept", "application/json");
        post.setHeader("X_LAYER_APP_ID", appId);

        JSONObject rootObject = new JSONObject();
        if (requestIdentityToken) {
            rootObject.put("nonce", nonce);
            rootObject.put("name", userName);
        } else {
            rootObject.put("name", "Web"); // name must be specified to make entiry valid
        }
        StringEntity entity = new StringEntity(rootObject.toString(), "UTF-8");
        entity.setContentType("application/json");
        post.setEntity(entity);

        HttpResponse response = (new DefaultHttpClient()).execute(post);
        if (HttpStatus.SC_OK != response.getStatusLine().getStatusCode()
                && HttpStatus.SC_CREATED != response.getStatusLine().getStatusCode()) {
            StringBuilder sb = new StringBuilder();
            sb.append("Got status ").append(response.getStatusLine().getStatusCode()).append(" [")
                    .append(response.getStatusLine()).append("] when logging in. Request: ").append(url);
            if (requestIdentityToken)
                sb.append(" login: ").append(userName).append(", nonce: ").append(nonce);
            Log.e(TAG, sb.toString());
            return new String[] { null, sb.toString() };
        }

        String responseString = EntityUtils.toString(response.getEntity());
        JSONObject jsonResp = new JSONObject(responseString);

        JSONArray atlasIdentities = jsonResp.getJSONArray("atlas_identities");
        List<Participant> participants = new ArrayList<Participant>(atlasIdentities.length());
        for (int i = 0; i < atlasIdentities.length(); i++) {
            JSONObject identity = atlasIdentities.getJSONObject(i);
            Participant participant = new Participant();
            participant.firstName = identity.getString("name");
            participant.userId = identity.getString("id");
            participants.add(participant);
        }
        if (participants.size() > 0) {
            setParticipants(participants);
            save();
            if (debug)
                Log.d(TAG, "refreshContacts() contacts: " + atlasIdentities);
        }

        if (requestIdentityToken) {
            String error = jsonResp.optString("error", null);
            String identityToken = jsonResp.optString("identity_token");
            return new String[] { identityToken, error };
        }
        return new String[] { null, "Refreshed " + participants.size() + " contacts" };
    } catch (Exception e) {
        Log.e(TAG, "Error when fetching identity token", e);
        return new String[] { null, "Cannot obtain identity token. " + e };
    }
}

From source file:com.layer.atlas.messenger.AtlasIdentityProvider.java

private boolean save() {
    Collection<Participant> participants;
    synchronized (participantsMap) {
        participants = participantsMap.values();
    }//w w w.ja v a2s.  c o  m

    JSONArray contactsJson;
    try {
        contactsJson = new JSONArray();
        for (Participant participant : participants) {
            JSONObject contactJson = new JSONObject();
            contactJson.put("id", participant.userId);
            contactJson.put("first_name", participant.firstName);
            contactJson.put("last_name", participant.lastName);
            contactsJson.put(contactJson);
        }
    } catch (JSONException e) {
        Log.e(TAG, "Error while saving", e);
        return false;
    }

    SharedPreferences.Editor editor = context.getSharedPreferences("contacts", Context.MODE_PRIVATE).edit();
    editor.putString("json", contactsJson.toString());
    editor.commit();

    return true;
}