Found 2616 Articles for Java

How to get all the keys of a JSON object using GSON in Java?

raja
Updated on 04-Jul-2020 08:33:51

3K+ Views

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 exampleExampleimport 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();   ... Read More

How to serialize and de-serialize generic types using the Gson library in Java?

raja
Updated on 04-Jul-2020 08:08:23

1K+ Views

If a Java class is a generic type and we are using it with the Gson library for JSON serialization and deserialization. The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.Syntaxpublic class TypeToken extends java.lang.ObjectExampleimport java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class GenericTypesJSONTest {    public static void main(String[] args) {       Gson gson = new GsonBuilder().setPrettyPrinting().create();       List list = Arrays.asList("INDIA", "AUSTRALIA", "ENGLAND", "SOUTH AFRICA");   ... Read More

Pretty print JSON using Jackson library in Java?

raja
Updated on 04-Jul-2020 08:02:26

2K+ Views

A Jackson API is a java based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We can process a JSON in three different ways using Streaming API, Tree Model, and Data Binding.We can Pretty print JSON using the writerWithDefaultPrettyPrinter() of ObjectMapper class, it is a factory method for constructing ObjectWriter that will serialize objects using the default pretty printer for indentation.Syntaxpublic ObjectWriter writerWithDefaultPrettyPrinter()Exampleimport java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; public class PrettyPrintJsonJacksonTest {    public static void main(String[] args) ... Read More

Convert JSON to/from Map using Jackson library in Java?

raja
Updated on 04-Jul-2020 07:58:38

2K+ Views

The JSON Jackson is a library for Java and it has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can convert JSON to/from Map using readValue() and writeValueAsString() methods of com.fasterxml.jackson.databind.ObjectMapper class.JSON to MapSyntaxpublic T readValue(String content, TypeReference valueTypeRef) throws IOException, JsonParseException, JsonMappingExceptionExampleimport java.io.*; import java.util.*; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.type.TypeReference; public class ConvertJSONToMapTest {    public static void main(String args[]) {       try {         ObjectMapper mapper = new ObjectMapper();          String jsonString = "{\"Name\":\"Raja\", \"Technology\":\"Java\"}";     ... Read More

How to convert a JSON array to CSV in Java?

raja
Updated on 04-Jul-2020 07:49:16

5K+ Views

The JSON can be used as a data-interchange format and is lightweight and language independent. A JSONArray can parse text from a String to produce a vector-like object and supports java.util.List interface. We can convert a JSON Array to CSV format using org.json.CDL class, it can provide a static method toString(),  to convert a JSONArray into comma-delimited text. We need to import org.apache.commons.io.FileUtils package to store the data in a CSV file using the writeStringToFile() method.Syntaxpublic static java.lang.String toString(JSONArray ja) throws JSONExceptionIn the below example, we can convert a JSON Array to CSV format.Exampleimport java.io.File; import org.apache.commons.io.FileUtils; import org.json.*; public class ConvertJsonToCSVTest {    public static void main(String[] ... Read More

How to pretty print JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:29:36

3K+ Views

A Gson is a JSON library for java, which is created by Google. By using Gson, we can generate JSON and convert JSON to java objects. By default, Gson can print the JSON in compact format. To enable Gson pretty print, we must configure the Gson instance using the setPrettyPrinting() method of GsonBuilder class and this method configures Gson to output JSON that fits in a page for pretty printing.Syntaxpublic GsonBuilder setPrettyPrinting()Exampleimport java.util.*; import com.google.gson.*; public class PrettyJSONTest {    public static void main( String[] args ) {       Employee emp = new Employee("Raja", "115", "Content Engineer", "Java", "Hyderabad");     ... Read More

Convert a Map to JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:19:48

3K+ Views

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 our Java program.We can create a Gson instance in two waysBy using new Gson().By creating a GsonBuilder instance and calling with the create() method.In the below program, we can convert a Map to a JSON object.Exampleimport java.lang.reflect.*; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class ConverMapToJsonTest {    public static ... Read More

Convert a list of objects to JSON using the Gson library in Java?

raja
Updated on 04-Jul-2020 07:14:39

7K+ Views

A Gson is a library that can be used to convert Java Objects to JSON representation. It can also be used to convert a JSON string to an equivalent Java object. The primary class to use is Gson which we can create by calling the new Gson() and the GsonBuilder class can be used to create a Gson instance.We can convert a list of objects by first creating a Person class and convert a list of Person objects to JSON.Exampleimport java.util.*; import java.util.stream.*; import com.google.gson.*; public class JSONConverterTest {    public static void main( String[] args ) {       Gson gson = new ... Read More

Convert JSON object to Java object using Gson library in Java?

raja
Updated on 04-Jul-2020 07:03:16

8K+ Views

A Gson is a json library for java, which is created by Google and it can be used to generate a JSON. By using Gson, we can generate JSON and convert JSON to java objects. We can call the fromJson() method of Gson class to convert a JSON object to Java Object.Syntaxpublic fromJson(java.lang.String json, java.lang.Class classOfT) throws JsonSyntaxExceptionExampleimport com.google.gson.*; public class JSONtoJavaObjTest {    public static void main(String[] args) {       Gson gson = new Gson();       Emp emp = gson.fromJson("{'name':'raja', 'age':25}", Emp.class);       System.out.println(emp.getName());       System.out.println(emp.getAge());    } } // Emp class class Emp ... Read More

How can we check if a JSON object is empty or not in Java?

raja
Updated on 04-Jul-2020 06:54:20

8K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is a key with value pair. The JSONObject can parse a text from a String to produce a map-like object and supports java.util.Map interface. We can check whether the JSON object is empty or not in the below exampleExampleimport java.util.*; import org.json.*; public class JSONObjectTest {    public static void main(String[] args) {       JSONObject jsonObj = new JSONObject(          "{" +             "Name : Jai," +             "Age : 25, " +             "Salary: 25000.00 " +          "}"       );       if(jsonObj.isEmpty()) {          System.out.println("JSON is empty");       } else {          System.out.println("JSON is not empty");       }    } }OutputJSON is not empty

Advertisements