// searchResult refers to the current element in the array "search_result" JSONObject questionMark = searchResult.getJSONObject("question_mark"); Iterator keys = questionMark.keys(); while(keys.hasNext()) { // loop to get the dynamic key String currentDynamicKey = (String)keys.next(); // get the value of the dynamic key JSONObject currentDynamicValue = questionMark.getJSONObject(currentDynamicKey); // do something here with the value... }
/** * Produce a JSONArray containing the names of the elements of this * JSONObject. * * @return A JSONArray containing the key strings, or null if the JSONObject * is empty. */ public JSONArray names() { JSONArray ja = new JSONArray(); Iterator<String> keys = this.keys(); while (keys.hasNext()) { ja.put(keys.next()); } return ja.length() == 0 ? null : ja; }
protected static void parseJSON(JSONObject json, MapObject o) { if (json.has("name")) { o.name = json.getString("name"); } if (json.has("enName")) { o.enName = json.getString("enName"); } if (json.has("names")) { JSONObject namesObj = json.getJSONObject("names"); o.names = new HashMap<>(); Iterator<String> iterator = namesObj.keys(); while (iterator.hasNext()) { String key = iterator.next(); String value = namesObj.getString(key); o.names.put(key, value); } } if (json.has("lat") && json.has("lon")) { o.location = new LatLon(json.getDouble("lat"), json.getDouble("lon")); } if (json.has("id")) { o.id = json.getLong("id"); } } }
JSONObject verticeObj = opObject.getJSONObject("input vertices:"); for (String pos : JSONObject.getNames(verticeObj)) { String vertexName = verticeObj.getString(pos); JSONObject keys = opObject.getJSONObject("keys:"); Vertex v = parentVertexes.iterator().next(); parentVertexes.clear(); if (v.outputOps.size() == 1) { for (int index = 0; index < conditionMap.length(); index++) { JSONObject cond = conditionMap.getJSONObject(index); String k = (String) cond.keys().next(); JSONObject condObject = new JSONObject((String)cond.get(k)); String type = condObject.getString("type"); String left = condObject.getString("left"); String right = condObject.getString("right"); if (keys.length() != 0) { sb.append(posToOpId.get(left) + "." + keys.get(left) + "=" + posToOpId.get(right) + "." + keys.get(right) + "(" + type + "),"); } else { for (int index = 0; index < conditionMap.length(); index++) { JSONObject cond = conditionMap.getJSONObject(index); String k = (String) cond.keys().next(); JSONObject condObject = new JSONObject((String)cond.get(k)); String type = condObject.getString("type"); String left = condObject.getString("left");
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Query post = RemoteAccess.evaluate(request); // String siteurl = request.getRequestURL().toString(); // String baseurl = siteurl.substring(0, siteurl.length() - // request.getRequestURI().length()) + request.getContextPath() + "/"; JSONObject TopMenuJsonObject = new TopMenuService().serviceImpl(post, null, null, null); JSONArray sitesarr = TopMenuJsonObject.getJSONArray("items"); response.setCharacterEncoding("UTF-8"); PrintWriter sos = response.getWriter(); sos.print(sitemaphead + "\n"); for (int i = 0; i < sitesarr.length(); i++) { JSONObject sitesobj = sitesarr.getJSONObject(i); Iterator<String> sites = sitesobj.keys(); sos.print("<url>\n<loc>" + sitesobj.getString(sites.next().toString()) + "/</loc>\n" + "<changefreq>weekly</changefreq>\n</url>\n"); } sos.print("</urlset>"); sos.println(); post.finalize(); } }
/** * cleanup deletes all old entries and frees up the memory. * some outside process muss call this frequently * @return self */ public Accounting cleanup() { if (!this.has("requests")) return this; JSONObject requests = this.getJSONObject("requests"); for (String path: requests.keySet()) { JSONObject events = requests.getJSONObject(path); // shrink that map and delete everything which is older than now minus one hour long pivotTime = System.currentTimeMillis() - ONE_HOUR_MILLIS; while (events.length() > 0 && Long.parseLong(events.keys().next()) < pivotTime) events.remove(events.keys().next()); if (events.length() == 0) requests.remove(path); } return this; }
String s = "{menu:{\"1\":\"sql\", \"2\":\"android\", \"3\":\"mvc\"}}"; JSONObject jObject = new JSONObject(s); JSONObject menu = jObject.getJSONObject("menu"); Map<String,String> map = new HashMap<String,String>(); Iterator iter = menu.keys(); while(iter.hasNext()){ String key = (String)iter.next(); String value = menu.getString(key); map.put(key,value); }
/** * Get an array of field names from a JSONObject. * * @return An array of field names, or null if there are no names. */ public static String[] getNames(JSONObject jo) { int length = jo.length(); if (length == 0) { return null; } Iterator<String> iterator = jo.keys(); String[] names = new String[length]; int i = 0; while (iterator.hasNext()) { names[i] = iterator.next(); i += 1; } return names; }
SDKInfo sdkInfo = new SDKInfo(source.optString("sdk_version"), source.optString("sdk_name")); DeviceDetails deviceDetails = new AndroidDeviceDetails(source.optString("carrier")); String eventType = source.getString("event_type"); Long timestamp = source.getLong("timestamp"); Id uniqueId = Id.valueOf(source.getString("unique_id")); Long sessionDuration = null; JSONObject sessionJSON = source.getJSONObject("session"); if (sessionJSON != null) { sessionId = sessionJSON.getString("id"); sessionStart = sessionJSON.getLong("startTimestamp"); sessionStop = sessionJSON.optLong("stopTimestamp"); Iterator<String> keysIterator = attributesJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); attributes.put(key, attributesJSON.optString(key)); Iterator<String> keysIterator = metricsJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); try { metrics.put(key, metricsJSON.getDouble(key));
private static void putImageInBundleWithArrayFormat( Bundle parameters, int index, JSONObject image) throws JSONException{ Iterator<String> keys = image.keys(); while (keys.hasNext()) { String property = keys.next(); String key = String.format(Locale.ROOT, "image[%d][%s]", index, property); parameters.putString(key, image.get(property).toString()); } }
final SDKInfo sdkInfo = new SDKInfo(source.optString("sdk_name"), source.optString("sdk_version")); final AndroidDeviceDetails deviceDetails = new AndroidDeviceDetails(source.optString("carrier")); final String eventId = source.getString("event_id"); final String eventType = source.getString("event_type"); final Long timestamp = source.getLong("timestamp"); final String uniqueId = source.getString("unique_id"); Long sessionDuration = null; final JSONObject sessionJSON = source.getJSONObject("session"); if (sessionJSON != null) { sessionId = sessionJSON.getString("id"); final Iterator<String> keysIterator = attributesJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); attributes.put(key, attributesJSON.optString(key)); final Iterator<String> keysIterator = metricsJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); try { metrics.put(key, metricsJSON.getDouble(key));
/** * Collects all keys in {@code jsonObject}. * * @param jsonObject the {@link JSONObject} to get the keys of * @return the set of keys */ public static Set<String> getKeys(JSONObject jsonObject) { Set<String> keys = new TreeSet<String>(); Iterator<?> iter = jsonObject.keys(); while (iter.hasNext()) { keys.add((String) iter.next()); } return keys; }
private Map<String, Object> toMap(JSONObject o) { Map<String, Object> map = new LinkedHashMap<>(); // https://github.com/jwtk/jjwt/issues/380: use .keys() and *not* .keySet() for Android compatibility: Iterator<String> iterator = o.keys(); while (iterator.hasNext()) { String key = iterator.next(); Object value = o.get(key); value = convertIfNecessary(value); map.put(key, value); } return map; }
private String calculateChecksum() { // JSONObject.toString() doesn't guarantee order of the keys on KitKat // (API Level 19) and below as JSONObject used HashMap internally, // starting Android API Level 20+, JSONObject changed to use LinkedHashMap if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) { return md5Checksum(jsonObject.toString()); } ArrayList<String> keys = new ArrayList<>(); for (Iterator<String> iterator = jsonObject.keys(); iterator.hasNext();) { keys.add(iterator.next()); } Collections.sort(keys); StringBuilder sb = new StringBuilder(); for (String key : keys) { sb.append(key).append(" = ").append(jsonObject.optString(key)).append('\n'); } return md5Checksum(sb.toString()); }
public static void jsonToMap(String t) throws JSONException { HashMap<String, String> map = new HashMap<String, String>(); JSONObject jObject = new JSONObject(t); Iterator<?> keys = jObject.keys(); while( keys.hasNext() ){ String key = (String)keys.next(); String value = jObject.getString(key); map.put(key, value); } System.out.println("json : "+jObject); System.out.println("map : "+map); }