Example usage for org.json JSONObject keys

List of usage examples for org.json JSONObject keys

Introduction

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

Prototype

public Iterator keys() 

Source Link

Document

Get an enumeration of the keys of the JSONObject.

Usage

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

@SuppressWarnings("unchecked")
public void deSerialize(Object jsonObject, Object targetObject) {

    if (jsonObject != null) {
        JSONObject jo = (JSONObject) jsonObject;
        Method[] methods = targetObject.getClass().getMethods();
        Iterator<String> it = jo.keys();
        while (it.hasNext()) {
            String key = it.next();
            // jump out if the key length is too short
            if (key.length() <= 1)
                continue;
            String mName = "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
            invokeMethod(methods, key, mName, jo, targetObject);
        }/*  w  w  w .j ava2 s  .c om*/
    }
}

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

@SuppressWarnings("unchecked")
private Object genericDeserialize(Object o, Object targetObject, String key) throws JSONException {

    if (o instanceof JSONObject) {
        JSONObject jo = (JSONObject) o;
        Map<String, Object> jaoo = new HashMap<String, Object>();

        // only add if we are not top level
        if (targetObject != null) {
            addValue(targetObject, jaoo, key);
        }/*  w w w. j av a  2s  . co  m*/
        Iterator<String> it = jo.keys();

        while (it.hasNext()) {
            String k = it.next();
            Object value = jo.get(k);
            genericDeserialize(value, jaoo, k);
        }
        // if we are the top level object return self
        if (targetObject == null) {
            return jaoo;
        }

    } else if (o instanceof JSONArray) {

        JSONArray ja = (JSONArray) o;

        List<Object> jal = new ArrayList<Object>();

        // only add if we are not top level
        if (targetObject != null) {
            addValue(targetObject, jal, key);
        }
        for (int i = 0; i < ja.length(); i++) {
            Object jao = ja.get(i);
            genericDeserialize(jao, jal, null);
        }
        // if we are the top level object return self
        if (targetObject == null) {
            return jal;
        }
        // primitives
    } else if (o instanceof Date) {
        Object value = ((Date) o).getTime();
        addValue(targetObject, value, key);
    } else {
        addValue(targetObject, o, key);
    }
    return null;
}

From source file:org.cvrgrid.waveform.backing.GlobusUploadBacking.java

/**
 * Private API that polls all the endpoints for a user.
 */// www .j  a va 2 s.  c o m
private Status getEndpoints(JSONTransferAPIClient client, String query, String endpointType) {

    try {

        JSONTransferAPIClient.Result r = client.getResult(query);
        Map<String, GlobusEndpointList> globusEndpointLists = this.getGlobusEndpointLists();
        GlobusEndpointList globusEndpointList = globusEndpointLists.get(endpointType);
        logger.info("Endpoint Listing " + query + " for " + client.getUsername() + ": ");
        Iterator<?> keys = r.document.keys();
        while (keys.hasNext()) {
            String next = (String) keys.next();
            if (next.equalsIgnoreCase("data_type")) {

                globusEndpointList.setDataType(r.document.getString(next));

            } else if (next.equalsIgnoreCase("length")) {

                globusEndpointList.setLength(new Integer(r.document.getString(next)));

            } else if (next.equalsIgnoreCase("limit")) {

                globusEndpointList.setLimit(r.document.getString(next));

            } else if (next.equalsIgnoreCase("offset")) {

                globusEndpointList.setOffset(r.document.getString(next));

            } else if (next.equalsIgnoreCase("total")) {

                globusEndpointList.setTotal(r.document.getString(next));

            } else if (next.equalsIgnoreCase("data")) {
                JSONArray data = r.document.getJSONArray(next);
                int size = data.length();
                ArrayList<GlobusEndpoint> globusEndpoints = new ArrayList<GlobusEndpoint>();
                for (int j = 0; j < size; j++) {
                    GlobusEndpoint globusEndpoint = new GlobusEndpoint();
                    JSONObject globusEndpointInfo = data.getJSONObject(j);
                    Iterator<?> keys2 = globusEndpointInfo.keys();
                    while (keys2.hasNext()) {
                        String next2 = (String) keys2.next();
                        if (next2.equalsIgnoreCase("data_type")) {

                            globusEndpoint.setDataType(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("username")) {

                            globusEndpoint.setUserName(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("globus_connect_setup_key")) {

                            globusEndpoint.setGlobusConnectSetupKey(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("name")) {

                            globusEndpoint.setName(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("activated")) {

                            globusEndpoint.setActivated(globusEndpointInfo.getBoolean(next2));

                        } else if (next2.equalsIgnoreCase("is_globus_connect")) {

                            globusEndpoint.setIsGlobusConnect(globusEndpointInfo.getBoolean(next2));

                        } else if (next2.equalsIgnoreCase("ls_link")) {

                            JSONObject linkInfo = globusEndpointInfo.getJSONObject(next2);
                            GlobusLink lsLink = new GlobusLink();
                            Iterator<?> keys3 = linkInfo.keys();
                            while (keys3.hasNext()) {

                                String next3 = (String) keys3.next();
                                if (next3.equalsIgnoreCase("data_type")) {

                                    lsLink.setDataType(linkInfo.getString(next3));

                                } else if (next3.equalsIgnoreCase("href")) {

                                    lsLink.setHref(linkInfo.getString(next3));

                                } else if (next3.equalsIgnoreCase("resource")) {

                                    lsLink.setResource(linkInfo.getString(next3));

                                } else if (next3.equalsIgnoreCase("relationship")) {

                                    lsLink.setRelationship(linkInfo.getString(next3));

                                } else if (next3.equalsIgnoreCase("title")) {

                                    lsLink.setTitle(linkInfo.getString(next3));

                                }

                            }
                            globusEndpoint.setLsLink(lsLink);

                        } else if (next2.equalsIgnoreCase("canonical_name")) {

                            globusEndpoint.setCanonicalName(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("myproxy_server")) {

                            globusEndpoint.setMyProxyServer(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("expire_time")) {

                            //globusEndpoint.setExpireTime(new Date(globusEndpointInfo.getString(next2)));

                        } else if (next2.equalsIgnoreCase("public")) {

                            globusEndpoint.setGlobusPublic(globusEndpointInfo.getBoolean(next2));

                        } else if (next2.equalsIgnoreCase("description")) {

                            globusEndpoint.setDescription(globusEndpointInfo.getString(next2));

                        } else if (next2.equalsIgnoreCase("data")) {

                            JSONArray serverData = globusEndpointInfo.getJSONArray(next2);
                            int serverDataSize = serverData.length();
                            ArrayList<GlobusServer> globusServers = new ArrayList<GlobusServer>();
                            for (int k = 0; k < serverDataSize; k++) {
                                GlobusServer globusServer = new GlobusServer();
                                JSONObject globusServerInfo = serverData.getJSONObject(k);
                                Iterator<?> keys4 = globusServerInfo.keys();
                                while (keys4.hasNext()) {
                                    String next4 = (String) keys4.next();
                                    if (next4.equalsIgnoreCase("data_type")) {

                                        globusServer.setDataType(globusServerInfo.getString(next4));

                                    } else if (next4.equalsIgnoreCase("id")) {

                                        globusServer.setId(globusServerInfo.getInt(next4));

                                    } else if (next4.equalsIgnoreCase("hostname")) {

                                        globusServer.setHostname(globusServerInfo.getString(next4));

                                    } else if (next4.equalsIgnoreCase("uri")) {

                                        globusServer.setUri(globusServerInfo.getString(next4));

                                    } else if (next4.equalsIgnoreCase("scheme")) {

                                        globusServer.setScheme(globusServerInfo.getString(next4));

                                    } else if (next4.equalsIgnoreCase("port")) {

                                        if (globusServerInfo.get("port").toString().equalsIgnoreCase("null")) {

                                            globusServer.setPort(0);

                                        } else {

                                            globusServer.setPort(globusServerInfo.getInt(next4));

                                        }

                                    } else if (next4.equalsIgnoreCase("subject")) {

                                        globusServer.setSubject(globusServerInfo.getString(next4));

                                    } else if (next4.equalsIgnoreCase("is_connected")) {

                                        globusServer.setIsConnected(globusServerInfo.getBoolean(next4));

                                    }

                                }
                                globusServers.add(globusServer);
                            }
                            globusEndpoint.setGlobusServers(globusServers);
                        }

                    }
                    globusEndpoints.add(globusEndpoint);
                }
                globusEndpointList.setGlobusEndpoints(globusEndpoints);
            }

        }

        globusEndpointLists.put(endpointType, globusEndpointList);
        this.setGlobusEndpointLists(globusEndpointLists);
        return Status.OK;

    } catch (Exception e) {

        logger.error("Got an exception..\n");
        logger.error(e.getMessage());
        logger.error(e.getStackTrace().toString());
        e.printStackTrace();
        return Status.FAIL;

    }

}

From source file:org.openqa.selenium.logging.SessionLogHandler.java

/**
 * Creates a session logs map, with session logs mapped to session IDs, given
 * a raw session log map as a JSON object.
 *  /*  www.j ava  2  s.  co  m*/
 * @param rawSessionMap The raw session map.
 * @return The session logs mapped to session IDs.
 * @throws Exception If something goes wrong in server communication or JSON parsing.
 */
public static Map<String, SessionLogs> getSessionLogs(JSONObject rawSessionMap) throws JSONException {
    Map<String, SessionLogs> sessionLogsMap = new HashMap<String, SessionLogs>();
    for (Iterator keyItr = rawSessionMap.keys(); keyItr.hasNext();) {
        String sessionId = (String) keyItr.next();
        SessionLogs sessionLogs = SessionLogs.fromJSON(rawSessionMap.getJSONObject(sessionId));
        sessionLogsMap.put(sessionId, sessionLogs);
    }
    return sessionLogsMap;
}

From source file:com.microsoft.mimickeralarm.utilities.Loggable.java

public void putJSON(JSONObject json) {
    try {// w  w  w .j  av a2 s. co  m
        Iterator<?> keys = json.keys();

        while (keys.hasNext()) {
            String key = (String) keys.next();
            Properties.put(key, json.get(key));
        }
    } catch (JSONException ex) {
        Logger.trackException(ex);
    }
}

From source file:io.github.grahambell.taco.TacoTransport.java

/**
 * Convert JSON object to Java Map./*from  w w  w .  jav a2  s. co m*/
 *
 * Each entry in the object is converted using the {@link #jsonToObject}
 * method.
 *
 * @param json the JSON object
 * @return Java Map representation of the object
 * @throws TacoException on error in conversion
 */
public Map<String, Object> jsonToMap(JSONObject json) throws TacoException {
    Map map = new HashMap<String, Object>();

    for (Iterator<String> i = json.keys(); i.hasNext();) {
        String key = i.next();
        map.put(key, jsonToObject(json.get(key)));
    }

    return map;
}

From source file:fr.arnaudguyon.xmltojsonlib.JsonToXml.java

private void prepareObject(Node node, JSONObject json) {
    Iterator<String> keyterator = json.keys();
    while (keyterator.hasNext()) {
        String key = keyterator.next();
        Object object = json.opt(key);
        if (object != null) {
            if (object instanceof JSONObject) {
                JSONObject subObject = (JSONObject) object;
                String path = node.getPath() + "/" + key;
                Node subNode = new Node(key, path);
                node.addChild(subNode);/*from   w  ww  . j av a  2  s  .c o m*/
                prepareObject(subNode, subObject);
            } else if (object instanceof JSONArray) {
                JSONArray array = (JSONArray) object;
                prepareArray(node, key, array);
            } else {
                String path = node.getPath() + "/" + key;
                // JSON numbers are represented either Integer or Double (IEEE 754)
                // Long may be represented in scientific notation because they are stored as Double
                // This workaround attempts to represent Long and Double objects accordingly
                String value;
                if (object instanceof Double) {
                    double d = (double) object;
                    // If it is a Long
                    if (d % 1 == 0) {
                        value = Long.toString((long) d);
                    } else {
                        // TODO: Set up number of decimal digits per attribute in the builder
                        // Set only once. Represent all double numbers up to 20 decimal digits
                        if (DECIMAL_FORMAT.getMaximumFractionDigits() == 0) {
                            DECIMAL_FORMAT.setMaximumFractionDigits(20);
                        }
                        value = DECIMAL_FORMAT.format(d);
                    }
                } else {
                    // Integer, Boolean and String are handled here
                    value = object.toString();
                }
                if (isAttribute(path)) {
                    node.addAttribute(key, value);
                } else if (isContent(path)) {
                    node.setContent(value);
                } else {
                    Node subNode = new Node(key, node.getPath());
                    subNode.setContent(value);
                    node.addChild(subNode);
                }
            }
        }
    }
}

From source file:com.leanengine.JsonDecode.java

private static Map<String, Object> accountPropsFromJson(JSONObject jsonNode)
        throws LeanException, JSONException {
    Map<String, Object> props = new HashMap<String, Object>(jsonNode.length());

    // must have some properties
    if (jsonNode.length() == 0)
        throw new LeanException(LeanError.Type.ServerError,
                "Malformed reply: JSON parameter 'providerProperties' must not be empty.");

    Iterator fieldNames = jsonNode.keys();
    while (fieldNames.hasNext()) {
        String field = (String) fieldNames.next();
        props.put(field, jsonNode.get(field));
    }/*from   ww w .  j a v  a 2s  .  c om*/
    return props;
}

From source file:com.leanengine.JsonDecode.java

static Map<String, Object> entityPropertiesFromJson(JSONObject jsonNode) throws LeanException, JSONException {
    Map<String, Object> props = new HashMap<String, Object>(jsonNode.length());

    // must have some properties
    if (jsonNode.length() == 0)
        throw new LeanException(LeanError.Type.ServerError, "Empty reply.");

    Iterator fieldNames = jsonNode.keys();
    while (fieldNames.hasNext()) {
        String field = (String) fieldNames.next();

        // skip LeanEngine system properties (starting with underscore '_')
        if (field.startsWith("_"))
            continue;
        Object subNode = jsonNode.get(field);
        props.put(field, propertyFromJson(subNode));
    }/*from   ww w.  j a  v a2  s  .  c o  m*/
    return props;
}

From source file:tv.yatse.plugin.avreceiver.sample.helpers.PreferencesHelper.java

public boolean importSettingsFromJSON(String settings, long version) {
    try {//from  ww  w.j a va2s . com
        JSONObject data = new JSONObject(settings);
        Iterator<String> keys = data.keys();

        SharedPreferences.Editor mEditor = mPreferences.edit();
        while (keys.hasNext()) {
            String key = keys.next();
            if (!TextUtils.equals(key, "settings_version")) {
                mEditor.putString(key, data.getString(key));
            }
        }
        mEditor.apply();
        settingsVersion(version);
    } catch (JSONException e) {
        YatseLogger.getInstance(mContext).logError(TAG, "Error decoding settings", e);
    }
    return true;
}