
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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
Found 208 Articles for JSON

2K+ Views
Sometimes, we may need to parse a JSON into a Gson tree model in Java. For example, we may need to parse a JSON string into a tree model when we want to manipulate the JSON data in a more easy way.The getAsJsonObject() Method The Gson library is used to parse a JSON String into a Tree Model. We will be using the JsonParser to parse the JSON string into a Tree Model of type JsonElement. We will be using the getAsJsonObject() method of JsonElement for getting the element as JsonObject, and the other is getAsJsonArray(), which is a method ... Read More

4K+ Views
When we have a JSON object and want to change a specific field name for some reason, we can use the Jackson library in Java.The Jackson Annotation @JsonProperty The Jackson Annotation @JsonProperty is used on a property or method during the serialization or deserialization of JSON. It takes an optional 'name' parameter, which is useful in case the property name is different from the key name in JSON. By default, if the key name matches the property name, the value is mapped to the property value. If the key name is different, we can use the @JsonProperty annotation to ... Read More

2K+ Views
The Jackson is a library for Java and it has very powerful data binding capabilities and it also provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. Ignoring Null and Empty Fields Using Jackson The Jackson library provides @JsonInclude annotation, which controls the serialization of a class as a whole or its individual fields based on their values during serialization. The @JsonInclude annotation contains below two values: Include.NON_NULL: Indicates that only properties with not null values will be included in JSON. Include.NON_EMPTY: Indicates ... Read More

329 Views
Pretty printing JSON is a process of formatting our JSON data to make it more readable and good-looking.The prettyPrint() Method of Flexjson We will be using the Flexjson library to pretty print JSON in Java. The Flexjson library is a lightweight library that is used for serializing as well as deserializing Java objects to and from JSON. A JSONSerializer is the main class for performing the serialization of Java objects to JSON and by default, performs a shallow serialization. We can pretty-print JSON using the prettyPrint(boolean prettyPrint) method of JSONSerializer class. Syntax of prettyPrint() The syntax of the prettyPrint() method ... Read More

5K+ Views
Deserializing a JSON to a Java object means converting a JSON string into a Java object.The deserialize() Method of the Flexjson Library We will be using the Flexjson library to deserialize a JSON to a Java object in Java. The Flexjson library is a lightweight library that is used for serializing as well as deserializing Java objects to and from JSON. A JSONDeserializer is the main class for performing deserialization of JSON to Java objects. We can deserialize a JSON string to a Java object using the deserialize(String json, Class type) method of JSONDeserializer. Syntax of deserialize() Method The syntax of the ... Read More

2K+ Views
While parsing a JSON String to or from a Java object, by default, Gson tries to create an instance of the Java class by calling the default constructor. In the case of Java, if a class doesn’t contain a default constructor or we want to do some initial configuration while creating Java objects, we need to create and register our own instance creator. Custom Instance Creator Using Gson Custom instance means creating a new instance that is not the default. In this instance, we can add whatever properties we want. There are many libraries that create custom instances. In this ... Read More

8K+ Views
Gson is a library in the Java that is mainly used for converting Java objects to JSON and vice versa. By default, the Gson object does not serialize the fields with null values to JSON. If a field in a Java object is null, Gson excludes it.Serializing a Null Field Using Gson We can force Gson to serialize null values via the GsonBuilder class. We need to call the serializeNulls() method on the GsonBuilder instance before creating the Gson object. Once serializeNulls() has been called, the Gson instance created by the GsonBuilder can include null fields in the serialized JSON. ... Read More

162 Views
Enabling version support means that the Gson library will be able to handle multiple versions of the same class. We use this when we have a class that has been modified and we want to be able to deserialize JSON data that was created using an older version of the class. Enabling Versioning Support in Java Gson Library The Gson library provides a simple versioning system for the Java objects that it reads and writes, and also provides an annotation named @Since for the versioning concept @Since(versionnumber). We can create a Gson instance with versioning using the GsonBuilder().setVersion() method. If ... Read More

7K+ Views
Gson is a JSON for Java, which is developed by Google. We can format a date using the Gson library in Java. The Gson library provides a GsonBuilder class that allows us to create a Gson instance with custom settings. We call the create() method of the GsonBuilder class to create an instance of the Gson class. Then we use the method setDateFormat() to configure Gson to serialize Date objects according to the pattern provided. In order to use the Gson library, we need to add it to our project. If you are using Maven, add this to your ... Read More

11K+ Views
Pretty Printing JSON using "org.json" Library Pretty printing something means formatting it in a way that is easy to read, understand, and also looks good. Here we will format JSON data in a good-looking as well as readable way. We will be using the org.json library to pretty print JSON data in Java. The method toString(int indentFactor) of the JSONObject class is used to pretty print JSON data. The indentFactor parameter specifies the number of spaces to add for each level of indentation. To use the org.json library, we need to add it to our project. If you are using ... Read More