Example usage for org.json JSONObject has

List of usage examples for org.json JSONObject has

Introduction

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

Prototype

public boolean has(String key) 

Source Link

Document

Determine if the JSONObject contains a specific key.

Usage

From source file:cn.code.notes.gtask.data.TaskList.java

public void setContentByRemoteJSON(JSONObject js) {
    if (js != null) {
        try {//from   ww  w .jav  a  2s .  c  om
            // id
            if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
                setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
            }

            // last_modified
            if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
                setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
            }

            // name
            if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
                setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
            }

        } catch (JSONException e) {
            Log.e(TAG, e.toString());
            e.printStackTrace();
            throw new ActionFailureException("fail to get tasklist content from jsonobject");
        }
    }
}

From source file:cn.code.notes.gtask.data.TaskList.java

public void setContentByLocalJSON(JSONObject js) {
    if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
        Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
    }/* www.  ja va 2s. c  o  m*/

    try {
        JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);

        if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
            String name = folder.getString(NoteColumns.SNIPPET);
            setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name);
        } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
            if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER)
                setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT);
            else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER)
                setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE);
            else
                Log.e(TAG, "invalid system folder");
        } else {
            Log.e(TAG, "error type");
        }
    } catch (JSONException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }
}

From source file:nl.hnogames.domoticz.Domoticz.UpdateParser.java

@Override
public void parseResult(String result) {
    try {/*from   w w  w.  j av  a2s  .  c o  m*/
        JSONObject response = new JSONObject(result);
        String version = "";
        if (response.has("revision"))
            version = response.getString("revision");
        if (response.has("HaveUpdate") && !response.getBoolean("HaveUpdate"))
            version = "";

        receiver.onReceiveUpdate(version);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

From source file:org.dspace.globus.Globus.java

public static String getAllMetadata(Context context, Item item, Collection collection) {
    try {//  w  w w  .j  a v  a2s  . c  o  m
        JSONObject dataset = new JSONObject();
        if (collection == null) {
            collection = item.getOwningCollection();
        }
        Community community = collection.getCommunities()[0];
        EPerson person = item.getSubmitter();

        dataset.put("_comment", "This file was auto-generated by Globus");
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "submitter"), person.getGlobusUserName());
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "collection"), collection.getName());
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "collection_id"), collection.getID());
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "community"), community.getName());
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "community_id"), community.getID());
        dataset.put(Globus.makeJSONLDKey("globus", "publication", "item_id"), item.getID());

        Set<String> schemas = new HashSet<String>();
        schemas.add("globus");
        String key;
        DCValue[] dcv = item.getMetadata(Item.ANY, Item.ANY, Item.ANY, Item.ANY);
        for (DCValue dcval : dcv) {
            // Make a unique key for this piece of metadata
            // if its a DC term we map to datacite
            if (dublinCoreDataciteMap.containsKey(dcval.getField())) {
                key = dublinCoreDataciteMap.getProperty(dcval.getField());
                schemas.add("datacite");
            } else {
                key = Globus.makeJSONLDKey(dcval);
                schemas.add(dcval.schema);
            }
            // We assume all metadata values could be multivalued
            // There is no way to tell so we encode all in an array
            JSONArray valueArray = null;
            if (!dataset.has(key)) {
                valueArray = new JSONArray();
            } else {
                valueArray = dataset.getJSONArray(key);
            }
            valueArray.put(dcval.value);
            dataset.put(key, valueArray);
        }
        // create JSON-LD context heading
        JSONObject jsonLDContext = new JSONObject();
        MetadataSchema metadataSchema;
        for (String s : schemas) {
            metadataSchema = MetadataSchema.find(context, s);
            jsonLDContext.put(s, metadataSchema.getNamespace());
        }
        dataset.put("@context", jsonLDContext);
        return dataset.toString();
    } catch (Exception e) {
        logger.error("Error getting item metadata " + e);
    }
    return "";
}

From source file:com.richtodd.android.quiltdesign.block.Quilt.java

static final Quilt createFromJSONObject(JSONObject jsonObject) throws JSONException {

    int rowCount = jsonObject.optInt("rowCount", 0);
    int columnCount = jsonObject.optInt("columnCount", 0);
    float width = (float) jsonObject.optDouble("width", 0);
    float height = (float) jsonObject.optDouble("height", 0);

    Quilt quilt = new Quilt(rowCount, columnCount, width, height);

    if (jsonObject.has("quiltBlocks")) {
        JSONArray jsonQuiltBlocks = jsonObject.getJSONArray("quiltBlocks");

        int index = -1;
        for (int row = 0; row < quilt.m_rowCount; ++row) {
            for (int column = 0; column < quilt.m_columnCount; ++column) {
                index += 1;/*www  .j  av  a  2 s .c om*/
                if (index < jsonQuiltBlocks.length()) {
                    JSONObject jsonQuiltBlock = jsonQuiltBlocks.optJSONObject(index);
                    if (jsonQuiltBlock != null) {
                        QuiltBlock quiltBlock = QuiltBlock.createFromJSONObject(jsonQuiltBlock);
                        quilt.setQuiltBlock(row, column, quiltBlock);
                    }
                }
            }
        }
    }

    quilt.m_new = false;
    return quilt;
}

From source file:com.microsoft.services.sharepoint.OfficeEntity.java

/**
 * List from json./*  ww w  .j  av a  2 s  .com*/
 * 
 * @param <E>
 *            the element type
 * @param json
 *            the json
 * @param clazz
 *            the clazz
 * @return the list
 * @throws org.json.JSONException
 *             the JSON exception
 */
protected static <E extends OfficeEntity> List<E> listFromJson(JSONObject json, Class<E> clazz)
        throws JSONException {
    List<E> list = new ArrayList<E>();

    JSONArray results;
    if (json.has("d")) {
        results = json.getJSONObject("d").getJSONArray("results");
    } else {
        results = json.getJSONArray("results");
    }

    for (int i = 0; i < results.length(); i++) {
        JSONObject result = results.getJSONObject(i);

        E item = null;
        try {
            item = clazz.newInstance();
        } catch (Throwable e) {
        }

        if (item != null) {
            item.loadFromJson(result);
            list.add(item);
        }
    }

    return list;
}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String noteCall(String message, String subject, String friendID) throws EasyFacebookError {

    String postID = null;//from  w  ww. j  a  v  a  2  s. c o  m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);
    params.putString("subject", subject);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/notes", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

public String commetReply(String message, String commentID) throws EasyFacebookError {

    String postID = null;/*ww  w  .  jav  a 2  s. c  om*/

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + commentID + "/comments", "POST",
                params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String postLocation(String message, String friendID, String longitude, String latitude,
        String pagePlaceID, ArrayList<String> userIdTag) throws EasyFacebookError {

    String postID = null;//www . j  a  v a 2 s. co  m

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);
    params.putString("coordinates",
            "{\"latitude\": \"" + latitude + "\", \"longitude\": \"" + longitude + "\"}");
    params.putString("place", pagePlaceID);

    String tag = "";
    int flagtag = 0;
    for (String idtag : userIdTag) {
        tag = tag + "," + idtag;
        flagtag = 1;
    }

    if (flagtag == 1) {
        tag = tag.substring(1, tag.length());
    }

    params.putString("tags", tag);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/checkins", "POST",
                params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}

From source file:com.easy.facebook.android.apicall.GraphApi.java

private String postCall(String message, String friendID, String urlPicture, String actionsUrl,
        String actionsName, String link, String name, String privacy, String countries, String regions,
        String locales, String description) throws EasyFacebookError {

    String postID = null;/* w w  w .java  2 s .c o  m*/

    Bundle params = new Bundle();
    params.putString("format", "json");
    params.putString("access_token", facebook.getAccessToken());
    params.putString("message", message);

    if (description != null)
        params.putString("description", description);

    if (actionsUrl != null && actionsName != null)
        params.putString("actions", "{\"name\": \"" + actionsName + "\", \"link\": \"" + actionsUrl + "\"}");

    if (name != null)
        params.putString("name", name);

    if (link != null)
        params.putString("link", link);

    if (privacy != null)
        params.putString("privacy", privacy);

    if (countries != null && regions != null && locales != null)
        params.putString("targeting", "{\"countries\": \"" + countries + "\", \"regions\": \"" + regions
                + "\", \"locales\": \"" + locales + "\"}");

    if (urlPicture != null)
        params.putString("picture", urlPicture);

    if (friendID == null)
        friendID = "me";

    try {

        String jsonResponse = Util.openUrl("https://graph.facebook.com/" + friendID + "/feed", "POST", params);

        JSONObject objectJSONErrorCheck = new JSONObject(jsonResponse);

        if (!objectJSONErrorCheck.isNull("error")) {
            throw new EasyFacebookError(jsonResponse);

        }

        JSONObject json = new JSONObject(jsonResponse);

        if (json.has("id"))
            postID = json.get("id").toString();

    } catch (MalformedURLException e) {

        throw new EasyFacebookError(e.toString(), "MalformedURLException");
    } catch (IOException e) {

        throw new EasyFacebookError(e.toString(), "IOException");
    } catch (JSONException e) {

        throw new EasyFacebookError(e.toString(), "JSONException");
    }

    return postID;

}