
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get all the keys of a JSON object using GSON in Java?
A Gson is a library that can be used to parse Java objects to JSON and vice-versa. It can also be used to convert a JSON string to an equivalent Java object. In order to parse java object to JSON or JSON to java object, we need to import com.google.gson package in the Java program.
We can get all the keys of a JSON object in the below example
Example
import java.util.*; import com.google.gson.*; import org.json.*; public class GetJSONAllKeysTest { public static void main(String[] args) { String jsonStr = "{\"Raja\":\"Java\", \"Ravi\":\"SAP\", \"Chaitanya\":\"Python\", \"Adithya\":\"Spark\"}"; JsonParser parser = new JsonParser(); JsonElement element = parser.parse(jsonStr); JsonObject obj = element.getAsJsonObject(); Set<Map.Entry<String, JsonElement>> entries = obj.entrySet(); for(Map.Entry<String, JsonElement> entry: entries) { System.out.println(entry.getKey()); } } }
Output
Raja Ravi Chaitanya Adithya
- Related Questions & Answers
- How to parse a JSON without duplicate keys using Gson in Java?
- Convert JSON object to Java object using Gson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert Java object to JSON using GSON library?
- How to rename the properties of JSON using Gson in Java?
- Convert a Map to JSON using the Gson library in Java?
- How to pretty print JSON using the Gson library in Java?
- Convert a list of objects to JSON using the Gson library in Java?
- How to write a JSON string to file using the Gson library in Java?
- How to get the size of a json object in JavaScript?
- How to parse a JSON to Gson Tree Model in Java?
- How to create a JSON Object using Object Model in Java?
- How to get the values of the different types from a JSON object in Java?
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to convert the JSON object to a bean using JSON-lib API in Java?
Advertisements