Java 使用 keySet() 从 JSONObject 中提取密钥
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19195492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Extracting Keys from a JSONObject using keySet()
提问by Rahil Arora
I'm trying to extract the keys from a JSON Object. The JSON object, in this case, is obtained by making an API call to a social networking site called SkyRockand looks like this :
我正在尝试从 JSON 对象中提取密钥。在这种情况下,JSON 对象是通过对名为SkyRock 的社交网站进行 API 调用获得的,如下所示:
{
"max_page": 2,
"posts": {
"3111623007": {
"id_post": 3111623007,
"media_align": "float_left",
"tags": [],
"nb_comments": 24
},
"3114564209": {
"id_post": 3114564209,
"media_align": "float_left",
"tags": [],
"nb_comments": 33
},
"3116902311": {
"id_post": 3116902311,
"media_align": "float_left",
"tags": [],
"nb_comments": 29
}
}
}
I basically want to store all the post_idvalues in an ArrayList. In order to do this, am trying to extract the keysfrom the JSON object and am doing this as follows:
我基本上想将所有post_id值存储在一个 ArrayList 中。为了做到这一点,我试图从 JSON 对象中提取密钥,并按如下方式执行此操作:
JSONObject posts = (JSONObject) jo.get("posts");
ArrayList<String> keys = (ArrayString<String>) posts.keyset();
The problem is that am not able to find a suitable variable type in which I can store the result obtained from the keyset()method.
问题是我无法找到合适的变量类型来存储从keyset()方法获得的结果。
I tried searching for the answers, but in most of the cases, keys()is being used to extract the keys (which am not able to use for some reason and I think it's maybe because am using org.json.simple, but am not sure).
我尝试寻找答案,但在大多数情况下,keys()用于提取密钥(由于某种原因无法使用,我认为这可能是因为我使用的是 org.json.simple,但是我没有把握)。
Can anyone please help me out here to find a solution to the problem or any alternate method to retrieve the Key values?
任何人都可以在这里帮我找到问题的解决方案或任何替代方法来检索键值吗?
Thanks.
谢谢。
采纳答案by JB Nizet
The javadocsays:
javadoc说:
public interface JsonObject
extends JsonStructure, Map<String,JsonValue>
So, a JSONObject is a Map whose keys are of type String
, and whose values are of type JSONValue
.
所以,一个 JSONObject 是一个 Map,它的键是 type String
,它的值是 type JSONValue
。
And the javadoc of Map<K, V>.keySet()
says:
Set<K> keySet()
Returns a Set view of the keys contained in this map
So, what JSONObject.keySet()
returns is a Set<String>
(which is quite logical, since keys of JSON objects are strings).
因此,JSONObject.keySet()
返回的是 a Set<String>
(这是非常合乎逻辑的,因为 JSON 对象的键是字符串)。
So all that you want is:
所以你想要的只是:
Set<String> keys = posts.keySet();
回答by Maxim Shoustin
The posts
represents Map
of JSONObject
where key
is String
该posts
代表Map
的JSONObject
地方key
是String
JSONObject mainObject = new JSONObject(jsonString);
JSONObject posts = mainObject.getJSONObject("posts");
Map<String, JSONObject> map = (Map<String,JSONObject>)posts.getMap();
ArrayList<String> list = new ArrayList<String>(map.keySet());
System.out.println(list);
Output:
输出:
[3116902311, 3114564209, 3111623007]
回答by alex
Recursive methods for extracting keys from json objects and arrays (in case of duplicates will merge them since methods return Set, but not Array)
从 json 对象和数组中提取键的递归方法(在重复的情况下将合并它们,因为方法返回 Set,而不是 Array)
public static Set<String> getAllKeys(JSONObject json) {
return getAllKeys(json, new HashSet<>());
}
public static Set<String> getAllKeys(JSONArray arr) {
return getAllKeys(arr, new HashSet<>());
}
private static Set<String> getAllKeys(JSONArray arr, Set<String> keys) {
for (int i = 0; i < arr.length(); i++) {
Object obj = arr.get(i);
if (obj instanceof JSONObject) keys.addAll(getAllKeys(arr.getJSONObject(i)));
if (obj instanceof JSONArray) keys.addAll(getAllKeys(arr.getJSONArray(i)));
}
return keys;
}
private static Set<String> getAllKeys(JSONObject json, Set<String> keys) {
for (String key : json.keySet()) {
Object obj = json.get(key);
if (obj instanceof JSONObject) keys.addAll(getAllKeys(json.getJSONObject(key)));
if (obj instanceof JSONArray) keys.addAll(getAllKeys(json.getJSONArray(key)));
}
keys.addAll(json.keySet());
return keys;
}
回答by Juan Salamanca
This works for me
这对我有用
o is a JSONObject -> import org.json.simple.JSONObject;
o 是一个 JSONObject -> 导入 org.json.simple.JSONObject;
Set<?> s = o.keySet();
Iterator<?> i = s.iterator();
do{
String k = i.next().toString();
System.out.println(k);
}while(i.hasNext());
回答by Flash
My JSONOject is jsonOject;
我的 JSONOject 是 jsonOject;
Iterator <String> listKEY = jsonOject.keys();
do {
String newKEY = listKEY.next().toString();
}while (listKEY.hasNext());