JavaGSON:获取JSONObject下所有键的列表

JavaGSON:获取JSONObject下所有键的列表,java,json,gson,jsonobject,Java,Json,Gson,Jsonobject,我在Java中使用GSON作为JSON解析器,但密钥并不总是相同的。 例如我有以下JSON: {“我已经知道的对象”:{ “键1”:“值1”, “键2”:“值2”, “AnotherObject”:{“anotherKey1”:“anotherValue1”,“anotherKey2”:“anotherValue2”} } 我已经得到了JSONObject“我已经知道的对象”。现在我需要得到这个对象的所有JSONElements,这将是“Key1”、“Key2”和“AnotherObject”。

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

{“我已经知道的对象”:{
“键1”:“值1”,
“键2”:“值2”,
“AnotherObject”:{“anotherKey1”:“anotherValue1”,“anotherKey2”:“anotherValue2”}
}

我已经得到了JSONObject“我已经知道的对象”。现在我需要得到这个对象的所有JSONElements,这将是“Key1”、“Key2”和“AnotherObject”。
提前感谢。

编辑:输出应该是一个字符串数组,包含JSONObject的所有键

您可以使用JsonParser将Json转换为一个中间结构,该结构允许您检查Json内容

String yourJson = "{your json here}";
JsonElement element = JsonParser.parseString(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());
}
String yourJson=“{your json here}”;
JsonElement元素=JsonParser.parseString(yourJson);
JsonObject obj=element.getAsJsonObject();//因为您知道它是一个JsonObject
Set entries=obj.entrySet();//将返回对象的成员
对于(Map.Entry:entries){
System.out.println(entry.getKey());
}
String str=“{\'key1\':\'val1\',\'key2\':\'val2\'”;
JsonParser=新的JsonParser();
JsonObject jObj=(JsonObject)parser.parse(str);
列表键=新的ArrayList();
对于(条目e:jObj.entrySet()){
key.add(e.getKey());
}
//键包含jsonObject的键

由于Java 8,您可以使用流作为外观更好的替代方案:

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);
String str=“{\'key1\':\'val1\',\'key2\':\'val2\'”;
JsonParser=新的JsonParser();
JsonObject jObj=(JsonObject)parser.parse(str);
列表键=jObj.entrySet()
.stream()
.map(i->i.getKey())
.collect(收集器.toCollection(ArrayList::new));
keys.forEach(System.out::println);

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

String json=“{\'key1\':\'val\',\'key2\':\'val\'”;
JsonParser=新的JsonParser();
JsonObject=parser.parse(json.getAsJsonObject();
Set keys=jsonObject.keySet();

可能的重复可能会有帮助您的最终输出应该是什么?应该是,
“key1”、“key2”、“AnotherObject”
还是
“我已经知道的对象”、“key1”、“key2”、“AnotherObject”
?顺便说一句,我已经向GSON提交了一个issue and pull请求,以使其成为一个继承方法。任何支持都可能加快它的速度。为什么不使用obj.keySet()?obj.keySet()似乎是一个更好的选择,因为每个google包中都可能有非GSON-2.1默认值
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);
String json = "{\"key1\":\"val\", \"key2\":\"val\"}";

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

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