Raja has Published 760 Articles

How to implement custom JSON serialization with Gson in Java?

raja

raja

Updated on 04-Jul-2020 08:51:40

1K+ Views

A Gson library provides a way to specify custom serializers by registering a custom serializer with the GsonBuilder if we need a way to convert a java object to JSON. We can create a custom serializer by overriding the serialize() method of com.google.gson.JsonSerializer class.In the below example, the implementation of custom serialization of JSON.Exampleimport ... Read More

How can we add a JSONArray within JSONObject in Java?

raja

raja

Updated on 04-Jul-2020 08:50:58

4K+ Views

A JSONObject can parse text from a String to produce a map-like object and a JSONArray can parse text from a String to produce a vector-like object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() ... Read More

How to convert XML to JSON array in Java?

raja

raja

Updated on 04-Jul-2020 08:43:57

6K+ Views

A JSON is a lightweight data-interchange format and the format of JSON is like a key-value pair. We can convert XML to JSON array using org.json.XML class, this provides a static method, XML.toJSONObject() to convert XML to JSON array.Syntaxpublic static JSONObject toJSONObject(java.lang.String string) throws JSONExceptionIn the below example, converting XML to JSON arrayExampleimport ... Read More

How to write a JSON string to file using the Gson library in Java?

raja

raja

Updated on 04-Jul-2020 08:37:01

1K+ Views

A Gson is a library that can be used to convert Java Objects to JSON representation. 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 write a JSON string to file using ... Read More

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

raja

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 ... Read More

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

raja

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 ... Read More

Pretty print JSON using Jackson library in Java?

raja

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 ... Read More

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

raja

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, ... Read More

How to convert a JSON array to CSV in Java?

raja

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 ... Read More

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

raja

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 ... Read More

Advertisements