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:com.gizwits.framework.activity.BaseActivity.java

/**
 * ??// ww  w.  j a v a2 s  .  co m
 * 
 * @param map
 *            the map
 * @param json
 *            the json
 * @throws JSONException
 *             the JSON exception
 */
protected void inputDataToMaps(ConcurrentHashMap<String, Object> map, String json) throws JSONException {
    Log.i("revjson", json);
    JSONObject receive = new JSONObject(json);
    Iterator actions = receive.keys();
    while (actions.hasNext()) {

        String action = actions.next().toString();
        Log.i("revjson", "action=" + action);
        // 
        if (action.equals("cmd") || action.equals("qos") || action.equals("seq") || action.equals("version")) {
            continue;
        }
        JSONObject params = receive.getJSONObject(action);
        Log.i("revjson", "params=" + params);
        Iterator it_params = params.keys();
        while (it_params.hasNext()) {
            String param = it_params.next().toString();
            Object value = params.get(param);
            map.put(param, value);
            Log.i(TAG, "Key:" + param + ";value" + value);
        }
    }
}

From source file:com.geekandroid.sdk.sample.JPushReceiver.java

private static String printBundle(Bundle bundle) {
    StringBuilder sb = new StringBuilder();
    for (String key : bundle.keySet()) {
        if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
            sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
        } else if (key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)) {
            sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
        } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
            if (bundle.getString(JPushInterface.EXTRA_EXTRA).isEmpty()) {
                Log.i(TAG, "This message has no Extra data");
                continue;
            }/*from  w w w  .j  a v a  2 s  .c  o  m*/

            try {
                JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                Iterator<String> it = json.keys();

                while (it.hasNext()) {
                    String myKey = it.next().toString();
                    sb.append("\nkey:" + key + ", value: [" + myKey + " - " + json.optString(myKey) + "]");
                }
            } catch (JSONException e) {
                Log.e(TAG, "Get message extra JSON error!");
            }

        } else {
            sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
        }
    }
    return sb.toString();
}

From source file:org.mariotaku.twidere.extension.mediauploader.util.ParseUtils.java

public static Bundle jsonToBundle(final String string) {
    final Bundle bundle = new Bundle();
    if (string != null) {
        try {//w  ww  . j  av a  2s .c  om
            final JSONObject json = new JSONObject(string);
            final Iterator<?> it = json.keys();
            while (it.hasNext()) {
                final Object key_obj = it.next();
                if (key_obj == null) {
                    continue;
                }
                final String key = key_obj.toString();
                final Object value = json.get(key);
                if (value instanceof Boolean) {
                    bundle.putBoolean(key, json.optBoolean(key));
                } else if (value instanceof Integer) {
                    // Simple workaround for account_id
                    if (shouldPutLong(key)) {
                        bundle.putLong(key, json.optLong(key));
                    } else {
                        bundle.putInt(key, json.optInt(key));
                    }
                } else if (value instanceof Long) {
                    bundle.putLong(key, json.optLong(key));
                } else if (value instanceof String) {
                    bundle.putString(key, json.optString(key));
                } else {
                    Log.w(LOGTAG,
                            "Unknown type " + value.getClass().getSimpleName() + " in arguments key " + key);
                }
            }
        } catch (final JSONException e) {
            e.printStackTrace();
        } catch (final ClassCastException e) {
            e.printStackTrace();
        }
    }
    return bundle;
}

From source file:com.athena.network.CubeiaClient.java

public void sendCreateTableRequest(int stake, int agMin, int maxPlayer, int vipMin, JSONObject jobj) {
    unRegistryLobby();//from  w  ww .  j  av a  2  s.c  o m
    CreateTableRequestPacket packet = new CreateTableRequestPacket();
    packet.gameid = User.gameId;
    packet.seats = (byte) maxPlayer;
    packet.seq = 1;
    packet.invitees = new int[0];
    packet.params = new ArrayList<Param>();
    try {
        if (jobj == null) {
            jobj = new JSONObject();
        }
        jobj.put("gameId", User.gameId);
        jobj.put("Name", "nhao` zo^ !");
        jobj.put("Mark", stake);
        jobj.put("gaAGmeId", agMin);
        jobj.put("Vip", vipMin);
        jobj.put("Player", maxPlayer);

        @SuppressWarnings("unchecked")
        Iterator<String> i = jobj.keys();
        while (i.hasNext()) {
            String nparam = i.next();
            Object oparam = jobj.get(nparam);
            Param param = ParameterUtil.createParam(nparam, oparam);
            packet.params.add(param);
        }

    } catch (JSONException e) {
        e.printStackTrace();
    }

    send(packet);
}

From source file:produvia.com.scanner.DeviceCard.java

private void updateTableInfo() throws JSONException {
    mDetailsTableEntries = new ArrayList<>();

    String field = extractStringFromJson("ip", mDevice);
    if (!field.isEmpty()) {
        if (isBluetoothDevice())
            mDetailsTableEntries.add("ADDRESS");
        else//from  ww  w .ja  v a  2 s  .c o m
            mDetailsTableEntries.add("IP");
        mDetailsTableEntries.add(field);
    }
    field = extractStringFromJson("mac", mDevice);
    if (!field.isEmpty()) {
        mDetailsTableEntries.add("MAC");
        mDetailsTableEntries.add(field);
    }

    //now add the services:
    if (!mDevice.has("services"))
        return;
    JSONObject services = mDevice.getJSONObject("services");
    for (Iterator<String> iter = services.keys(); iter.hasNext();) {
        String service_id = iter.next();
        JSONObject service = services.getJSONObject(service_id);
        //if a device card is already present - just merge the data:
        field = extractStringFromJson("service", service);
        if (!field.isEmpty()) {
            mDetailsTableEntries.add(field);
            mDetailsTableEntries.add(buildStringFromFields(new String[] { "description" }, service));
        }
    }

}

From source file:produvia.com.scanner.DeviceCard.java

private static String mergeServices(JSONObject s1, JSONObject s2) throws JSONException {
    if (s1 == null && s2 == null)
        return null;
    if (s1 == null)
        return s2.toString();
    if (s2 == null)
        return s1.toString();

    //ok they're both not null - merge them:
    for (Iterator<String> iter = s1.keys(); iter.hasNext();) {
        String service_id = iter.next();
        if (s2.has(service_id))
            continue;
        s2.put(service_id, s1.getJSONObject(service_id));
    }//from w  w w.  j  a  v a 2s.  co m
    return s2.toString();
}

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

/**
 * see parent//from  w ww.  ja  v a 2 s.com
 */
@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.EQUIPPABLE_EQUIPPING, mEquippingModel.toString());
    } catch (JSONException e) {
        StoreUtils.LogError(TAG, "An error occurred while generating JSON object.");
    }

    return jsonObject;
}

From source file:org.official.json.Property.java

/**
 * Converts the JSONObject into a property file object.
 * @param jo JSONObject/*ww w . j ava2s .c o m*/
 * @return java.util.Properties
 * @throws JSONException
 */
public static Properties toProperties(JSONObject jo) throws JSONException {
    Properties properties = new Properties();
    if (jo != null) {
        Iterator<String> keys = jo.keys();
        while (keys.hasNext()) {
            String name = keys.next();
            properties.put(name, jo.getString(name));
        }
    }
    return properties;
}

From source file:org.jabsorb.ng.serializer.impl.MapSerializer.java

@Override
public ObjectMatch tryUnmarshall(final SerializerState state, final Class<?> clazz, final Object o)
        throws UnmarshallException {

    final JSONObject jso = (JSONObject) o;
    String java_class;

    // Hint presence
    try {/*  w  w  w  .j a v a  2s . co m*/
        java_class = jso.getString("javaClass");
    } catch (final JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }
    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }

    // Class compatibility check
    if (!classNameCheck(java_class)) {
        // FIXME: previous version also handled Properties
        throw new UnmarshallException("not a Map");
    }

    // JSON Format check
    JSONObject jsonmap;
    try {
        jsonmap = jso.getJSONObject("map");
    } catch (final JSONException e) {
        throw new UnmarshallException("Could not read map: " + e.getMessage(), e);
    }
    if (jsonmap == null) {
        throw new UnmarshallException("map missing");
    }

    // Content check
    final ObjectMatch m = new ObjectMatch(-1);
    state.setSerialized(o, m);

    final Iterator<?> i = jsonmap.keys();
    String key = null;
    try {
        while (i.hasNext()) {
            key = (String) i.next();
            m.setMismatch(ser.tryUnmarshall(state, null, jsonmap.get(key)).max(m).getMismatch());
        }
    } catch (final UnmarshallException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    } catch (final JSONException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    }
    return m;
}

From source file:org.jabsorb.ng.serializer.impl.MapSerializer.java

@Override
public Object unmarshall(final SerializerState state, final Class<?> clazz, final Object o)
        throws UnmarshallException {

    final JSONObject jso = (JSONObject) o;
    String java_class;

    // Hint check
    try {// www.j  av a  2  s  .c o  m
        java_class = jso.getString("javaClass");
    } catch (final JSONException e) {
        throw new UnmarshallException("Could not read javaClass", e);
    }

    if (java_class == null) {
        throw new UnmarshallException("no type hint");
    }

    // Create the map
    final Map<Object, Object> abmap;
    if (java_class.equals("java.util.Map") || java_class.equals("java.util.AbstractMap")
            || java_class.equals("java.util.HashMap")) {
        abmap = new HashMap<Object, Object>();
    } else if (java_class.equals("java.util.TreeMap")) {
        abmap = new TreeMap<Object, Object>();
    } else if (java_class.equals("java.util.LinkedHashMap")) {
        abmap = new LinkedHashMap<Object, Object>();
    } else if (java_class.equals("java.util.Properties")) {
        abmap = new Properties();
    } else {
        throw new UnmarshallException("not a Map");
    }

    // Parse the JSON map
    JSONObject jsonmap;
    try {
        jsonmap = jso.getJSONObject("map");
    } catch (final JSONException e) {
        throw new UnmarshallException("Could not read map: " + e.getMessage(), e);
    }
    if (jsonmap == null) {
        throw new UnmarshallException("map missing");
    }

    state.setSerialized(o, abmap);

    final Iterator<?> i = jsonmap.keys();
    String key = null;
    try {
        while (i.hasNext()) {
            key = (String) i.next();
            abmap.put(key, ser.unmarshall(state, null, jsonmap.get(key)));
        }
    } catch (final UnmarshallException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    } catch (final JSONException e) {
        throw new UnmarshallException("key " + key + " " + e.getMessage(), e);
    }

    return abmap;
}