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.collectionspace.chain.util.misc.JSON.java

@SuppressWarnings("unchecked")
public static Map<String, String> stringObjectToMap(JSONObject in) throws JSONException {
    Map<String, String> out = new HashMap<String, String>();
    Iterator ks = in.keys();
    while (ks.hasNext()) {
        String k = (String) ks.next();
        out.put(k, in.getString(k));/*from  w  w  w .j  a  v  a  2 s.  co m*/
    }
    return out;
}

From source file:com.vk.sdk.util.VKJsonHelper.java

/**
 * Converts selected json-object to map//w w w .  ja v  a  2  s  .  c o m
 *
 * @param object object to convert
 * @return Filled map
 * @throws JSONException
 */
public static Map<String, Object> toMap(JSONObject object) throws JSONException {
    Map<String, Object> map = new HashMap<String, Object>();
    Iterator keys = object.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        map.put(key, fromJson(object.get(key)));
    }
    return map;
}

From source file:com.basetechnology.s0.agentserver.config.AgentServerConfig.java

public void update(JSONObject json) throws AgentServerException, JSONException {
    // First validate the keys
    JsonUtils.validateKeys(json, "config",
            new ArrayList<String>(Arrays.asList("name", "description", "software", "version", "website",
                    "contact", "user_agent_name", "default_web_page_refresh_interval",
                    "minimum_web_page_refresh_interval", "minimum_web_site_access_interval",
                    "minimum_web_access_interval", "max_users", "max_instances", "execution_limit_level_1",
                    "execution_limit_level_2", "execution_limit_level_3", "execution_limit_level_4",
                    "execution_limit_default_level", "default_trigger_interval", "default_reporting_interval",
                    "minimum_trigger_interval", "minimum_reporting_interval", "implicitly_deny_web_access",
                    "implicitly_deny_web_write_access", "mail_access_enabled", "minimum_mail_access_interval",
                    "minimum_host_mail_access_interval", "minimum_address_mail_access_interval",
                    "admin_approve_user_create", "mail_confirm_user_create",
                    "default_limit_instance_states_stored", "maximum_limit_instance_states_stored",
                    "default_limit_instance_states_returned", "maximum_limit_instance_states_returned")));

    // Now simply copy the keys to the config map
    for (Iterator<String> it = json.keys(); it.hasNext();) {
        String key = it.next();//from  w  w  w .jav a  2 s.co  m
        // TODO/Note: This will update persistence one key at a time
        put(key, json.getString(key));
    }
}

From source file:com.tassadar.multirommgr.installfragment.UbuntuChannel.java

public UbuntuChannel(String channelName, String fullName, JSONObject c) throws JSONException {
    m_name = channelName;//from w w  w  .  j av a  2s.  co m
    m_fullName = fullName;
    m_alias = c.optString("alias", null);

    JSONObject dev = c.getJSONObject("devices");
    Iterator itr = dev.keys();
    while (itr.hasNext()) {
        String code = (String) itr.next();
        JSONObject d = dev.getJSONObject(code);

        m_devices.put(code, d.getString("index"));
    }
}

From source file:cgeo.geocaching.connector.oc.OkapiClient.java

private static List<Geocache> parseCaches(final JSONObject response) {
    try {//from   www.j ava2  s .  c o m
        // Check for empty result
        final String result = response.getString("results");
        if (StringUtils.isBlank(result) || StringUtils.equals(result, "[]")) {
            return Collections.emptyList();
        }

        // Get and iterate result list
        final JSONObject cachesResponse = response.getJSONObject("results");
        if (cachesResponse != null) {
            final List<Geocache> caches = new ArrayList<Geocache>(cachesResponse.length());
            @SuppressWarnings("unchecked")
            final Iterator<String> keys = cachesResponse.keys();
            while (keys.hasNext()) {
                final String key = keys.next();
                final Geocache cache = parseSmallCache(cachesResponse.getJSONObject(key));
                caches.add(cache);
            }
            return caches;
        }
    } catch (final JSONException e) {
        Log.e("OkapiClient.parseCachesResult", e);
    }
    return Collections.emptyList();
}

From source file:com.balch.mocktrade.finance.QuoteGoogleFinance.java

public static QuoteGoogleFinance fromJSONObject(JSONObject jsonObject) throws Exception {
    QuoteGoogleFinance quote = new QuoteGoogleFinance();
    Iterator iter = jsonObject.keys();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        if (!jsonObject.isNull(key)) {
            quote.data.put(key, jsonObject.getString(key));
        }//  www . j a  va2 s  .c  o  m
    }

    return quote;

}

From source file:ch.icclab.cyclops.resource.impl.RateResource.java

/**
 * Saves static rate into InfluxDB//from   w w  w . jav a2  s. c o  m
 *
 * Pseudo Code
 * 1. Populate object array with "rate" data from JSONObject
 * 2. Create the POJO object from object array
 * 3. Map POJO object to JSON data
 * 4. Save JSON data in InfluxDB client
 *
 * @param jsonObj Data obj containing the static rate of a resource
 * @param ratingPolicy String containing the rating policy
 * @return boolean
 */
private boolean saveStaticRate(JSONObject jsonObj, String ratingPolicy) {
    boolean result = false;
    TSDBData rateData = new TSDBData();
    ArrayList<String> strArr = new ArrayList<String>();
    ArrayList<Object> objArrNode;
    ArrayList<ArrayList<Object>> objArr = new ArrayList<ArrayList<Object>>();
    ObjectMapper mapper = new ObjectMapper();
    Load load = new Load();
    HashMap staticRate = new HashMap();
    String jsonData = null;
    JSONObject rateJsonObj;
    Iterator iterator;
    String key;

    if (dbClient == null) {
        dbClient = new InfluxDBClient();
    }
    //TODO: simplify this code, separate method of object creation from this method
    // Reset the hashmap containing the static rate
    load.setStaticRate(staticRate);
    // Set the flag to "Static"
    Flag.setMeteringType("static");
    // Create the Array to hold the names of the columns
    strArr.add("resource");
    strArr.add("rate");
    strArr.add("rate_policy");
    try {
        rateJsonObj = (JSONObject) jsonObj.get("rate");
        iterator = rateJsonObj.keys();
        while (iterator.hasNext()) {
            key = (String) iterator.next();
            objArrNode = new ArrayList<Object>();
            objArrNode.add(key);
            objArrNode.add(rateJsonObj.get(key));
            objArrNode.add(ratingPolicy);
            objArr.add(objArrNode);
            // Load the rate hashmap with the updated rate
            staticRate.put(key, rateJsonObj.get(key));
        }
    } catch (JSONException e) {
        logger.error("Error while saving the Static Rate: " + e.getMessage());
        e.printStackTrace();
    }
    // Update the static hashmap containing the static rates
    load.setStaticRate(staticRate);
    // Set the data object to save into the DB
    rateData.setName("rate");
    rateData.setColumns(strArr);
    rateData.setPoints(objArr);
    try {
        jsonData = mapper.writeValueAsString(rateData);
    } catch (JsonProcessingException e) {
        logger.error("Error while saving the Static Rate: " + e.getMessage());
        e.printStackTrace();
    }
    result = dbClient.saveData(jsonData);
    return result;
}

From source file:org.uiautomation.ios.IOSCapabilities.java

public IOSCapabilities(JSONObject json) throws JSONException {
    Iterator<String> iter = json.keys();
    while (iter.hasNext()) {
        String key = iter.next();
        Object value = json.get(key);
        setCapability(key, decode(value));
    }/*from  w w  w.  ja va2 s . com*/
}

From source file:de.itomig.itoplib.GetItopJSON.java

public static <T> ArrayList<T> getArrayFromJson(String json, Type T) {
    ArrayList<T> list = new ArrayList<T>();
    String code = "100"; // json error code, "0" => everything is o.k.
    Log.d(TAG, "getArrayFromJson - Type=" + T.toString());

    JSONObject jsonObject = null;//from www  . j  av  a  2s .  c  om
    try {
        jsonObject = new JSONObject(json);
        code = jsonObject.getString("code");
        Log.d(TAG, "code=" + code);
        Log.d(TAG, "message=" + jsonObject.getString("message"));

    } catch (JSONException e) {
        Log.e(TAG, "error in getArrayFromJSON " + e.getMessage());
    }

    if ((jsonObject != null) && (code.trim().equals("0"))) {
        try {

            JSONObject objects = jsonObject.getJSONObject("objects");
            Iterator<?> keys = objects.keys();

            while (keys.hasNext()) {
                String key = (String) keys.next();
                Log.d(TAG, "key=" + key);
                if (objects.get(key) instanceof JSONObject) {
                    // Log.d(TAG,"obj="+objects.get(key).toString());
                    JSONObject o = (JSONObject) objects.get(key);
                    JSONObject fields = o.getJSONObject("fields");
                    Log.d(TAG, "fields=" + fields.toString());

                    Gson gson = new Gson();
                    String k[] = key.split(":");
                    int id = Integer.parseInt(k[2]);
                    T jf = gson.fromJson(fields.toString(), T);
                    if (jf instanceof CMDBObject) {
                        ((CMDBObject) jf).id = id;
                    }
                    list.add(jf);
                }
            }
            Log.d(TAG, "code=" + jsonObject.getString("code"));
            Log.d(TAG, "message=" + jsonObject.getString("message"));

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } // endif (jsonObject != null)

    return list;

}

From source file:com.soomla.store.domain.virtualGoods.UpgradeVG.java

/**
 * see parent//from  w ww  . j av a  2  s .  c o  m
 */
@Override
public JSONObject toJSONObject() {
    JSONObject parentJsonObject = super.toJSONObject();
    JSONObject jsonObject = new JSONObject();
    try {
        Iterator<?> keys = parentJsonObject.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            jsonObject.put(key, parentJsonObject.get(key));
        }

        jsonObject.put(JSONConsts.VGU_GOOD_ITEMID, mGoodItemId);
        jsonObject.put(JSONConsts.VGU_PREV_ITEMID, TextUtils.isEmpty(mPrevItemId) ? "" : mPrevItemId);
        jsonObject.put(JSONConsts.VGU_NEXT_ITEMID, TextUtils.isEmpty(mNextItemId) ? "" : mNextItemId);
    } catch (JSONException e) {
        StoreUtils.LogError(TAG, "An error occurred while generating JSON object.");
    }

    return jsonObject;
}