Skip to content

Instantly share code, notes, and snippets.

@ashwinrs
Last active January 9, 2023 23:54
Show Gist options
  • Save ashwinrs/75bc701c2576d1f42087b539404a7a7b to your computer and use it in GitHub Desktop.
Save ashwinrs/75bc701c2576d1f42087b539404a7a7b to your computer and use it in GitHub Desktop.
Iterate over elements in a JSON using GSON in Java
final JsonObject jsonObject = GSON.toJsonTree(<Object>).getAsJsonObject();
for(Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) {
System.out.println("Key = " + entry.getKey() + " Value = " + entry.getValue() );
}
@AdnanHussainTurki
Copy link

Great. Working like fluid.

@sagarnishant1
Copy link

In java i am getting an error over .entrySet()

@sagarnishant1
Copy link

Nevermind. I had to add .getAsJsonObject() as it was JsonElement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment