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:org.eclipse.orion.server.tests.servlets.git.GitStashTest.java

protected String getStashLocation(JSONObject project) throws Exception {
    JSONObject clone = getCloneForGitResource(project);
    assertTrue(clone.has(GitConstants.KEY_STASH));
    return clone.getString(GitConstants.KEY_STASH);
}

From source file:es.rocapal.wtl.api.WTLManager.java

private Response parseResponse(String jsonResponse) {
    JSONObject json;

    if (jsonResponse == null)
        return null;

    try {//  w w  w. j a v a 2 s. c o  m
        json = new JSONObject(jsonResponse);
        if (json.has(CODE_KEY) && json.has(MSG_KEY))
            return new Response(json.getInt(CODE_KEY), json.getString(MSG_KEY));
        else
            return null;

    } catch (JSONException e) {
        Log.e(TAG, e.getMessage());
        return null;
    }

}

From source file:com.atolcd.alfresco.helper.AuditHelper.java

/**
 * Extract the action that triggered the activity
 * /*from www  . ja  v  a  2 s . com*/
 * @param json
 * @return String Type
 * @throws JSONException
 */
public static String extractActionFromActivity(JSONObject json) throws JSONException {
    String type = null;
    if (json.has("type")) {
        String[] tokens = json.getString("type").split("\\.");
        if (tokens.length > 0) {
            type = tokens[tokens.length - 1];
        }
    }
    return type;
}

From source file:com.atolcd.alfresco.helper.AuditHelper.java

/**
 * Extract the module concerned by the activity
 * /*from  w w w  .  j  av  a  2s.  c o  m*/
 * @param json
 * @return String Module
 * @throws JSONException
 */
public static String extractModFromActivity(JSONObject json) throws JSONException {
    String mod = null;
    if (json.has("appTool")) {
        String tool = json.getString("appTool");
        if ("datalists".equals(tool)) {
            mod = AuditFilterConstants.MOD_DOCUMENT;
        } else if ("documentlibrary".equals(tool)) {
            mod = AuditFilterConstants.MOD_DOCUMENT;
        }
    }
    return mod;
}

From source file:org.protorabbit.json.DefaultSerializer.java

@SuppressWarnings("unchecked")
void invokeMethod(Method[] methods, String key, String name, JSONObject jo, Object targetObject) {
    Object param = null;/*  w  w  w.j  ava2  s . c om*/
    Throwable ex = null;
    for (int i = 0; i < methods.length; i++) {
        Method m = methods[i];
        if (m.getName().equals(name)) {
            Class<?>[] paramTypes = m.getParameterTypes();
            if (paramTypes.length == 1 && jo.has(key)) {
                Class<?> tparam = paramTypes[0];
                boolean allowNull = false;
                try {
                    if (jo.isNull(key)) {
                        // do nothing because param is already null : lets us not null on other types
                    } else if (Long.class.isAssignableFrom(tparam) || tparam == long.class) {
                        param = new Long(jo.getLong(key));
                    } else if (Double.class.isAssignableFrom(tparam) || tparam == double.class) {
                        param = new Double(jo.getDouble(key));
                    } else if (Integer.class.isAssignableFrom(tparam) || tparam == int.class) {
                        param = new Integer(jo.getInt(key));
                    } else if (String.class.isAssignableFrom(tparam)) {
                        param = jo.getString(key);
                    } else if (Enum.class.isAssignableFrom(tparam)) {
                        param = Enum.valueOf((Class<? extends Enum>) tparam, jo.getString(key));
                    } else if (Boolean.class.isAssignableFrom(tparam)) {
                        param = new Boolean(jo.getBoolean(key));
                    } else if (jo.isNull(key)) {
                        param = null;
                        allowNull = true;
                    } else if (Collection.class.isAssignableFrom(tparam)) {

                        if (m.getGenericParameterTypes().length > 0) {
                            Type t = m.getGenericParameterTypes()[0];
                            if (t instanceof ParameterizedType) {
                                ParameterizedType tv = (ParameterizedType) t;
                                if (tv.getActualTypeArguments().length > 0
                                        && tv.getActualTypeArguments()[0] == String.class) {

                                    List<String> ls = new ArrayList<String>();
                                    JSONArray ja = jo.optJSONArray(key);
                                    if (ja != null) {
                                        for (int j = 0; j < ja.length(); j++) {
                                            ls.add(ja.getString(j));
                                        }
                                    }
                                    param = ls;
                                } else if (tv.getActualTypeArguments().length == 1) {
                                    ParameterizedType type = (ParameterizedType) tv.getActualTypeArguments()[0];
                                    Class itemClass = (Class) type.getRawType();
                                    if (itemClass == Map.class && type.getActualTypeArguments().length == 2
                                            && type.getActualTypeArguments()[0] == String.class
                                            && type.getActualTypeArguments()[1] == Object.class) {

                                        List<Map<String, Object>> ls = new ArrayList<Map<String, Object>>();

                                        JSONArray ja = jo.optJSONArray(key);
                                        if (ja != null) {
                                            for (int j = 0; j < ja.length(); j++) {
                                                Map<String, Object> map = new HashMap<String, Object>();
                                                JSONObject mo = ja.getJSONObject(j);
                                                Iterator<String> keys = mo.keys();
                                                while (keys.hasNext()) {
                                                    String okey = keys.next();
                                                    Object ovalue = null;
                                                    // make sure we don't get JSONObject$Null
                                                    if (!mo.isNull(okey)) {
                                                        ovalue = mo.get(okey);
                                                    }
                                                    map.put(okey, ovalue);
                                                }
                                                ls.add(map);
                                            }
                                        }
                                        param = ls;
                                    } else {
                                        getLogger().warning(
                                                "Don't know how to handle Collection of type : " + itemClass);
                                    }

                                } else {
                                    getLogger().warning("Don't know how to handle Collection of type : "
                                            + tv.getActualTypeArguments()[0]);
                                }
                            }
                        }
                    } else {
                        getLogger().warning(
                                "Unable to serialize " + key + " :  Don't know how to handle " + tparam);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (param != null || allowNull) {

                    try {

                        if (m != null) {
                            Object[] args = { param };
                            m.invoke(targetObject, args);
                            ex = null;
                            break;
                        }
                    } catch (SecurityException e) {
                        ex = e;
                    } catch (IllegalArgumentException e) {
                        ex = e;
                    } catch (IllegalAccessException e) {
                        ex = e;
                    } catch (InvocationTargetException e) {
                        ex = e;
                    }
                }
            }
        }
    }
    if (ex != null) {
        if (ex instanceof RuntimeException) {
            throw (RuntimeException) ex;
        } else {
            throw new RuntimeException(ex);
        }
    }
}

From source file:com.whizzosoftware.hobson.scheduler.ical.ICalTaskProviderTest.java

@Test
public void testEditEvent() throws Exception {
    File file = File.createTempFile("hob", ".ics");
    try {/*from  w w  w .  ja  v  a  2s  . c o  m*/
        String ical = "BEGIN:VCALENDAR\n" + "PRODID:-//Whizzo Software//Hobson 1.0//EN\n" + "VERSION:2.0\n"
                + "BEGIN:VEVENT\n" + "UID:15dee4fe-a841-4cf6-8d7f-76c3ad5492b1\n" + "DTSTART:20130714T170000Z\n"
                + "DTEND:20130714T170000Z\n" + "SUMMARY:My Task\n"
                + "COMMENT:[{'pluginId':'com.whizzosoftware.hobson.server-api','actionId':'log','name':'My Action','properties':{'message':'foo'}}]\n"
                + "END:VEVENT\n" + "END:VCALENDAR";

        String ical2 = "BEGIN:VCALENDAR\n" + "PRODID:-//Whizzo Software//Hobson 1.0//EN\n" + "VERSION:2.0\n"
                + "BEGIN:VEVENT\n" + "UID:15dee4fe-a841-4cf6-8d7f-76c3ad5492b1\n" + "DTSTART:20130714T170000Z\n"
                + "DTEND:20130714T170000Z\n" + "SUMMARY:My Edited Task\n"
                + "COMMENT:[{'pluginId':'com.whizzosoftware.hobson.server-api','actionId':'log','name':'My Edited Action','properties':{'message':'foobar'}}]\n"
                + "END:VEVENT\n" + "END:VCALENDAR";

        // write out ICS to temp file
        FileWriter fw = new FileWriter(file);
        fw.append(ical);
        fw.close();

        ICalTaskProvider p = new ICalTaskProvider("pluginId", null, null,
                TimeZone.getTimeZone("America/Denver"));
        p.setScheduleExecutor(new MockScheduledTaskExecutor());
        p.setScheduleFile(file);
        p.start();

        // make sure the task was created
        assertEquals(1, p.getTasks().size());

        // create task JSON
        JSONObject json = new JSONObject();
        json.put("name", "My Edited Task");
        JSONArray conds = new JSONArray();
        json.put("conditions", conds);
        JSONObject cond = new JSONObject();
        conds.put(cond);
        cond.put("start", "20130714T170000Z");
        JSONArray actions = new JSONArray();
        json.put("actions", actions);
        JSONObject action = new JSONObject();
        actions.put(action);
        action.put("pluginId", "com.whizzosoftware.hobson.server-api");
        action.put("actionId", "log");
        action.put("name", "My Edited Action");
        JSONObject props = new JSONObject();
        action.put("properties", props);
        props.put("message", "foobar");

        // update the task
        p.updateTask("15dee4fe-a841-4cf6-8d7f-76c3ad5492b1", json);

        assertTrue(file.exists());

        // read back file
        Calendar cal = new CalendarBuilder().build(new FileInputStream(file));
        assertEquals(1, cal.getComponents().size());
        VEvent c = (VEvent) cal.getComponents().get(0);
        assertEquals("My Edited Task", c.getProperty("SUMMARY").getValue());
        assertEquals("15dee4fe-a841-4cf6-8d7f-76c3ad5492b1", c.getProperty("UID").getValue());
        assertEquals("20130714T170000Z", c.getProperty("DTSTART").getValue());
        JSONArray aj = new JSONArray(new JSONTokener(c.getProperty("COMMENT").getValue()));
        assertEquals(1, aj.length());
        JSONObject cj = aj.getJSONObject(0);
        assertEquals("com.whizzosoftware.hobson.server-api", cj.getString("pluginId"));
        assertEquals("My Edited Action", cj.getString("name"));
        assertEquals("log", cj.getString("actionId"));
        assertTrue(cj.has("properties"));
        JSONObject pj = cj.getJSONObject("properties");
        assertEquals("foobar", pj.getString("message"));
    } finally {
        file.delete();
    }
}

From source file:com.skubit.bitid.fragments.SignInResponseFragment.java

private String getMessageValue(String message) {
    if (TextUtils.isEmpty(message)) {
        return null;
    }/*from w w w.  j a va 2s.  c o  m*/

    try {
        JSONObject joMessage = new JSONObject(message);
        if (joMessage.has("message")) {
            return joMessage.get("message").toString();
        }
    } catch (JSONException e) {
    }
    return "Unknown Error";
}

From source file:com.github.irib_examples.Act_NetworkListView.java

private Response.Listener<JSONObject> createMyReqSuccessListener() {
    return new Response.Listener<JSONObject>() {
        @Override/* w w w .  j  a v a2 s  .c om*/
        public void onResponse(JSONObject response) {
            try {
                JSONObject feed = response.getJSONObject("feed");
                JSONArray entries = feed.getJSONArray("entry");
                JSONObject entry;
                for (int i = 0; i < entries.length(); i++) {
                    entry = entries.getJSONObject(i);

                    String url = null;

                    JSONObject media = entry.getJSONObject("media$group");
                    if (media != null && media.has("media$thumbnail")) {
                        JSONArray thumbs = media.getJSONArray("media$thumbnail");
                        if (thumbs != null && thumbs.length() > 0) {
                            url = thumbs.getJSONObject(0).getString("url");
                        }
                    }

                    mEntries.add(new PicasaEntry(entry.getJSONObject("title").getString("$t"), url));
                }
                mAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                showErrorDialog();
            }
        }
    };
}

From source file:nl.hnogames.domoticzapi.Parsers.MobileDeviceParser.java

@Override
public void parseResult(String result) {
    try {/*w ww  . j  a v a 2 s  .  c  o m*/
        JSONObject jsonResult = new JSONObject(result);
        if (jsonResult.has("status")) {
            if (jsonResult.getString("status").equals("OK")) {
                mobileReceiver.onSuccess();
                return;
            }
        }
        mobileReceiver.onError(null);
    } catch (JSONException e) {
        Log.e(TAG, "MobileDeviceParser JSON exception");
        e.printStackTrace();
        mobileReceiver.onError(e);
    }
}

From source file:org.gdg.bari.entities.TableTurn.java

static public TableTurn unpersist(byte[] byteArray) {

    if (byteArray == null) {
        Log.d(TAG, "Empty array---possible bug.");
        return new TableTurn();
    }/*w w w  .j ava 2 s . com*/

    String st = null;
    try {
        st = new String(byteArray, "UTF-16");
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
        return null;
    }

    Log.d(TAG, "====UNPERSIST \n" + st);

    TableTurn retVal = new TableTurn();

    try {
        JSONObject obj = new JSONObject(st);

        if (obj.has("data")) {
            retVal.data = obj.getString("data");
        }
        if (obj.has("turnCounter")) {
            retVal.turnCounter = obj.getInt("turnCounter");
        }

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

    return retVal;
}