org.json.JSONObject.keys()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(11.3k)|赞(0)|评价(0)|浏览(418)

本文整理了Java中org.json.JSONObject.keys()方法的一些代码示例,展示了JSONObject.keys()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。JSONObject.keys()方法的具体详情如下:
包路径:org.json.JSONObject
类名称:JSONObject
方法名:keys

JSONObject.keys介绍

[英]Returns an iterator of the String names in this object. The returned iterator supports Iterator#remove(), which will remove the corresponding mapping from this object. If this object is modified after the iterator is returned, the iterator's behavior is undefined. The order of the keys is undefined.
[中]返回此对象中字符串名称的迭代器。返回的迭代器支持迭代器#remove(),它将从此对象中删除相应的映射。如果在返回迭代器后修改此对象,则迭代器的行为未定义。键的顺序未定义。

代码示例

代码示例来源:origin: stackoverflow.com

// 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...
 }

代码示例来源:origin: commonsguy/cw-omnibus

@Override
public Object getGroup(int groupPosition) {
 @SuppressWarnings("rawtypes")
 Iterator i=model.keys();
 while (groupPosition > 0) {
  i.next();
  groupPosition--;
 }
 return(i.next());
}

代码示例来源:origin: zzz40500/GsonFormat

/**
 * 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;
}

代码示例来源:origin: osmandapp/Osmand

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");
    }
  }
}

代码示例来源:origin: apache/hive

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");

代码示例来源:origin: loklak/loklak_server

@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();
  }
}

代码示例来源:origin: loklak/loklak_server

/**
 * 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;
}

代码示例来源:origin: stackoverflow.com

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);
 }

代码示例来源:origin: zzz40500/GsonFormat

/**
 * 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;
}

代码示例来源:origin: aws-amplify/aws-sdk-android

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));

代码示例来源:origin: facebook/facebook-android-sdk

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());
  }
}

代码示例来源:origin: ACRA/acra

@NonNull
  public Map<String, Object> toMap() {
    final Map<String, Object> map = new HashMap<>(content.length());
    for (final Iterator<String> iterator = content.keys(); iterator.hasNext(); ) {
      final String key = iterator.next();
      map.put(key, get(key));
    }
    return map;
  }
}

代码示例来源:origin: aws-amplify/aws-sdk-android

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));

代码示例来源:origin: facebook/facebook-android-sdk

private static Bundle jsonToBundle(JSONObject jsonObject) throws JSONException {
  Bundle bundle = new Bundle();
  Iterator iter = jsonObject.keys();
  while (iter.hasNext()) {
    String key = (String) iter.next();
    String value = jsonObject.getString(key);
    bundle.putString(key,value);
  }
  return bundle;
}

代码示例来源:origin: org.skyscreamer/jsonassert

/**
 * 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;
}

代码示例来源:origin: jwtk/jjwt

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;
}

代码示例来源:origin: facebook/facebook-android-sdk

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());
}

代码示例来源:origin: stackoverflow.com

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);
 }

代码示例来源:origin: alibaba/Tangram-Android

private void parseBizParams(BaseCell cell, JSONObject json) {
  if (json == null) {
    return;
  }
  Iterator<String> iterator = json.keys();
  while (iterator.hasNext()) {
    String key = iterator.next();
    cell.addBizParam(key, json.opt(key));
  }
}

代码示例来源:origin: stackoverflow.com

JSONObject songs= json.getJSONObject("songs");
Iterator x = songs.keys();
JSONArray jsonArray = new JSONArray();

while (x.hasNext()){
  String key = (String) x.next();
  jsonArray.put(songs.get(key));
}

相关文章

JSONObject类方法