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:edu.utexas.cs.bevomaps.DataLayer.java

/**
 * Helper method to parse the JSON and insert into HashMaps
 *
 * @param buildingJSON subclass of ParseObject, stores building data in JSON format
 * @return This will return the HashMap of HashMaps or null if there is a problem
 */// www  .  ja v  a2 s  .  c o  m
private static HashMap<String, HashMap<String, String>> extractImageMap(BuildingJSON buildingJSON) {

    HashMap<String, HashMap<String, String>> imageMaps = new HashMap<>();
    JSONObject json = buildingJSON.getBuildingJSON();
    Iterator<String> iter = json.keys();
    while (iter.hasNext()) {
        String building = iter.next();
        JSONObject buildingInfo;
        try {
            buildingInfo = json.getJSONObject(building);
            HashMap<String, String> buildingInfoMap = new HashMap<>();
            Iterator<String> innerIter = buildingInfo.keys();
            while (innerIter.hasNext()) {
                String key = innerIter.next();
                buildingInfoMap.put(key, buildingInfo.getString(key));
            }
            imageMaps.put(building, buildingInfoMap);

        } catch (JSONException jsonException) {
            Log.e(TAG, jsonException.toString());
        }
    }

    if (imageMaps.size() > 0) {
        return imageMaps;
    } else
        return null;
}

From source file:edu.utexas.cs.bevomaps.DataLayer.java

static HashMap<String, String> getSearchMap() {

    HashMap<String, String> searchMap = new HashMap<>();
    ParseQuery<BuildingJSON> query = ParseQuery.getQuery("BuildingJSON");
    query = query.whereEqualTo("pk", "jsonObj");

    try {//from   ww w  .  j a v a  2s.  com
        BuildingJSON parseBuildingJSON = query.getFirst();
        if (parseBuildingJSON != null) {
            try {
                JSONObject json = parseBuildingJSON.getSearchMap();
                Iterator<String> iter = json.keys();
                while (iter.hasNext()) {
                    String key = iter.next();
                    searchMap.put(key, json.getString(key));
                }
            } catch (JSONException jsonException) {
                Log.e(TAG, jsonException.toString());
            }
        }
    } catch (ParseException e) {
        Log.e(TAG, e.toString());
    }

    return searchMap;
}

From source file:com.transistorsoft.cordova.bggeo.CDVBackgroundGeolocation.java

private boolean setConfig(JSONObject config) {
    try {/* w w w  .  j a v a  2 s  . c o m*/
        JSONObject merged = new JSONObject();
        JSONObject[] objs = new JSONObject[] { mConfig, config };
        for (JSONObject obj : objs) {
            Iterator it = obj.keys();
            while (it.hasNext()) {
                String key = (String) it.next();
                merged.put(key, obj.get(key));
            }
        }
        mConfig = merged;
    } catch (JSONException e) {
        e.printStackTrace();
        return false;
    }

    cordova.getThreadPool().execute(new Runnable() {
        public void run() {
            applyConfig();
            Bundle event = new Bundle();
            event.putString("name", BackgroundGeolocationService.ACTION_SET_CONFIG);
            event.putBoolean("request", true);
            postEvent(event);
        }
    });
    return true;
}

From source file:org.jdesktop.http.async.JsonHttpRequest.java

private void stuffIntoMap(JSONObject obj, Map<String, Object> map) {
    if (obj == null) {
        return;/* ww  w  .  j  av  a  2  s  .  c  o  m*/
    }

    Iterator itr = obj.keys();
    while (itr.hasNext()) {
        String key = (String) itr.next();
        Object value = obj.opt(key);

        if (value instanceof JSONArray) {
            JSONArray a = (JSONArray) value;
            Object[] array = new Object[a.length()];
            stuffIntoArray(a, array);
            map.put(key, array);
        } else if (value instanceof JSONObject) {
            Map<String, Object> submap = new HashMap<String, Object>();
            stuffIntoMap((JSONObject) value, submap);
            map.put(key, submap);
        } else {
            map.put(key, value);
        }
    }
}

From source file:com.soomla.store.domain.data.BalanceDrivenPriceModel.java

/**
 * docs in {@link AbstractPriceModel#toJSONObject()}
 *///from   w w w  .j a  va2s  .  c  o m
@Override
public JSONObject toJSONObject() throws JSONException {
    JSONObject parentJsonObject = super.toJSONObject();
    JSONObject jsonObject = new JSONObject();

    Iterator<?> keys = parentJsonObject.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        jsonObject.put(key, parentJsonObject.get(key));
    }

    JSONArray valuesPerBalance = new JSONArray();
    for (HashMap<String, Integer> currencyValue : mCurrencyValuePerBalance) {
        JSONObject currencyValues = new JSONObject();
        for (String key : currencyValue.keySet()) {
            currencyValues.put(key, currencyValue.get(key));
        }
        valuesPerBalance.put(currencyValues);
    }
    jsonObject.put(JSONConsts.GOOD_PRICE_MODEL_VALUES, valuesPerBalance);

    return jsonObject;
}

From source file:com.soomla.store.domain.data.BalanceDrivenPriceModel.java

/**
 * Creates a {@link BalanceDrivenPriceModel} with the given JSONObject.
 * @param jsonObject is a JSONObject representation of the required {@link BalanceDrivenPriceModel}.
 * @return an instance of {@link BalanceDrivenPriceModel}.
 * @throws JSONException//ww w .j  a  va  2  s  .  c  om
 */
public static BalanceDrivenPriceModel fromJSONObject(JSONObject jsonObject) throws JSONException {
    JSONArray valuesPerBalanceJSON = jsonObject.getJSONArray(JSONConsts.GOOD_PRICE_MODEL_VALUES);
    ArrayList<HashMap<String, Integer>> valuesPerBalance = new ArrayList<HashMap<String, Integer>>(
            valuesPerBalanceJSON.length());
    for (int i = 0; i < valuesPerBalanceJSON.length(); i++) {
        JSONObject valuesJSON = valuesPerBalanceJSON.getJSONObject(i);
        Iterator<?> valuesKeys = valuesJSON.keys();
        HashMap<String, Integer> values = new HashMap<String, Integer>();
        while (valuesKeys.hasNext()) {
            String key = (String) valuesKeys.next();
            values.put(key, valuesJSON.getInt(key));
        }

        valuesPerBalance.add(i, values);
    }

    return new BalanceDrivenPriceModel(valuesPerBalance);
}

From source file:net.algart.simagis.live.json.minimal.SimagisLiveUtils.java

public static Set<String> getKeySet(JSONObject jsonObject) {
    if (jsonObject == null) {
        return Collections.emptySet();
    }/*  w  w w  .j  ava  2 s  . co  m*/
    final TreeSet<String> result = new TreeSet<String>();
    final Iterator keys = jsonObject.keys();
    while (keys.hasNext()) {
        result.add((String) keys.next());
    }
    return Collections.unmodifiableSortedSet(result);
}

From source file:bpv.neurosky.connector.ThinkGearSocket.java

private void parsePacket(JSONObject data) {
    Iterator<?> itr = data.keys();
    while (itr.hasNext()) {

        Object e = itr.next();/* www.  ja v  a2 s . c om*/
        String key = e.toString();

        try {

            if (key.matches("poorSignalLevel")) {
                triggerPoorSignalEvent(data.getInt(e.toString()));

            }
            if (key.matches("rawEeg")) {
                int rawValue = (Integer) data.get("rawEeg");
                raw[index] = rawValue;
                index++;
                if (index == 512) {
                    index = 0;
                    int rawCopy[] = new int[512];

                    //TODO: Ajusta para funcionar na implementao atual
                    rawCopy = Arrays.copyOf(raw, raw.length);

                    triggerRawEvent(rawCopy);
                }
            }
            if (key.matches("blinkStrength")) {
                triggerBlinkEvent(data.getInt(e.toString()));

            }

            if (key.matches("eSense")) {
                JSONObject esense = data.getJSONObject("eSense");
                triggerAttentionEvent(esense.getInt("attention"));
                triggerMeditationEvent(esense.getInt("meditation"));

            }
            if (key.matches("eegPower")) {
                JSONObject eegPower = data.getJSONObject("eegPower");
                triggerEEGEvent(eegPower.getInt("delta"), eegPower.getInt("theta"), eegPower.getInt("lowAlpha"),
                        eegPower.getInt("highAlpha"), eegPower.getInt("lowBeta"), eegPower.getInt("highBeta"),
                        eegPower.getInt("lowGamma"), eegPower.getInt("highGamma"));

                //System.out.println(key);
            }

        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }

    // 
}

From source file:com.mparticle.internal.ConfigManager.java

public boolean shouldTrigger(MPMessage message) {
    JSONArray messageMatches = getTriggerMessageMatches();
    JSONArray triggerHashes = getTriggerMessageHashes();

    //always trigger for PUSH_RECEIVED
    boolean shouldTrigger = message.getMessageType().equals(Constants.MessageType.PUSH_RECEIVED)
            || message.getMessageType().equals(Constants.MessageType.COMMERCE_EVENT);

    if (!shouldTrigger && messageMatches != null && messageMatches.length() > 0) {
        shouldTrigger = true;// w  w w. j a va  2  s .c  o  m
        int i = 0;
        while (shouldTrigger && i < messageMatches.length()) {
            try {
                JSONObject messageMatch = messageMatches.getJSONObject(i);
                Iterator<?> keys = messageMatch.keys();
                while (shouldTrigger && keys.hasNext()) {
                    String key = (String) keys.next();
                    shouldTrigger = message.has(key);
                    if (shouldTrigger) {
                        try {
                            shouldTrigger = messageMatch.getString(key)
                                    .equalsIgnoreCase(message.getString(key));
                        } catch (JSONException stringex) {
                            try {
                                shouldTrigger = message.getBoolean(key) == messageMatch.getBoolean(key);
                            } catch (JSONException boolex) {
                                try {
                                    shouldTrigger = message.getDouble(key) == messageMatch.getDouble(key);
                                } catch (JSONException doubleex) {
                                    shouldTrigger = false;
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {

            }
            i++;
        }
    }
    if (!shouldTrigger && triggerHashes != null) {
        for (int i = 0; i < triggerHashes.length(); i++) {
            try {
                if (triggerHashes.getInt(i) == message.getTypeNameHash()) {
                    shouldTrigger = true;
                    break;
                }
            } catch (JSONException jse) {

            }
        }
    }
    return shouldTrigger;
}

From source file:com.mparticle.internal.ConfigManager.java

public Map<String, String> getIntegrationAttributes(int kitId) {
    Map<String, String> integrationAttributes = new HashMap<String, String>();
    JSONObject jsonAttributes = getIntegrationAttributes();
    if (jsonAttributes != null) {
        JSONObject kitAttributes = jsonAttributes.optJSONObject(Integer.toString(kitId));
        if (kitAttributes != null) {
            try {
                Iterator<String> keys = kitAttributes.keys();
                while (keys.hasNext()) {
                    String key = keys.next();
                    if (kitAttributes.get(key) instanceof String) {
                        integrationAttributes.put(key, kitAttributes.getString(key));
                    }//w  w  w .  jav  a2  s.  c o  m
                }
            } catch (JSONException e) {

            }
        }
    }
    return integrationAttributes;
}