Java GSON:获取 JSONObject 下所有键的列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31094305/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 10:37:45  来源:igfitidea点击:

Java GSON: Getting the list of all keys under a JSONObject

javajsongsonjsonobject

提问by imthe666st

I have got GSON as a JSON parser in Java, but the keys aren't always the same.
For example. I have the following JSON:

我有 GSON 作为 Java 中的 JSON 解析器,但密钥并不总是相同的。
例如。我有以下 JSON:

{ "The Object I already know": {
"key1":"value1",
"key2":"value2",
"AnotherObject": { "anotherKey1":"anotherValue1", "anotherKey2":"anotherValue2" }
}

{ "我已经知道的对象": {
"key1":"value1",
"key2":"value2",
"AnotherObject": { "anotherKey1":"anotherValue1", "anotherKey2":"anotherValue2" }
}

I have already got the JSONObject "The Object I already know". Now I need to get all of the JSONElements for this Object, this would be "Key1", "Key2" and "AnotherObject".
Thanks in advance.
EDIT: The Output should be a String Array with all the keys for the JSONObject

我已经得到了 JSONObject“我已经知道的对象”。现在我需要获取此对象的所有 JSONElements,这将是“Key1”、“Key2”和“AnotherObject”。
提前致谢。
编辑:输出应该是一个字符串数组,其中包含 JSONObject 的所有键

采纳答案by habsq

You can use JsonParser to convert your Json into an intermediate structure which allow you to examine the json content.

您可以使用 JsonParser 将您的 Json 转换为允许您检查 json 内容的中间结构。

String yourJson = "{your json here}";
JsonParser parser = new JsonParser();
JsonElement element = parser.parse(yourJson);
JsonObject obj = element.getAsJsonObject(); //since you know it's a JsonObject
Set<Map.Entry<String, JsonElement>> entries = obj.entrySet();//will return members of your object
for (Map.Entry<String, JsonElement> entry: entries) {
    System.out.println(entry.getKey());
}

回答by Raman Shrivastava

String str = "{\"key1\":\"val1\", \"key2\":\"val2\"}";

        JsonParser parser = new JsonParser();
        JsonObject jObj = (JsonObject)parser.parse(str);

        List<String> keys = new ArrayList<String>();
        for (Entry<String, JsonElement> e : jObj.entrySet()) {
            keys.add(e.getKey());
        }

        // keys contains jsonObject's keys

回答by Jaxt0r

Since Java 8 you can use Streams as better looking alternative:

从 Java 8 开始,您可以使用 Streams 作为更好看的替代方案:

String str = "{\"key1\":\"val1\", \"key2\":\"val2\"}";

JsonParser parser = new JsonParser();
JsonObject jObj = (JsonObject) parser.parse(str);

List<String> keys = jObj.entrySet()
    .stream()
    .map(i -> i.getKey())
    .collect(Collectors.toCollection(ArrayList::new));

keys.forEach(System.out::println);

回答by Michal Tenenberg

As of Gson 2.8.1 you can use keySet():

从 Gson 2.8.1 开始,您可以使用keySet()

String json = "{\"key1\":\"val\", \"key2\":\"val\"}";

JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse(json).getAsJsonObject();

Set<String> keys = jsonObject.keySet();